14

I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer. So, I have changed my question.

I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.

From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.

However, while using this, I faced one problem. It uses the GetOpenFileName function and OPENFILENAME structure from MFC. And OPENFILENAME has the member named "templateID". It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.

But I don't know How can I attach this file to my C# project?

Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?

Community
  • 1
  • 1
Yun
  • 5,233
  • 4
  • 21
  • 38
  • To download the *source* of the retired WindowsAPICodePack, which includes the CommonOpenFileDialog mentioned in some answers, see [Boltclock's answer here](https://stackoverflow.com/a/24420985/199364). – ToolmakerSteve Apr 01 '18 at 19:46
  • ... in that download, the `CommonOpenFileDialog` class is in Project **Shell** / `CommonFileDialogs` / `CommonOpenFileDialog.cs`. NOTE: Requires Windows Vista or newer. – ToolmakerSteve Apr 01 '18 at 19:56
  • See the **[Ookii Dialogs](https://github.com/ookii-dialogs)** which have an implementation of a folder browser dialog – C. Augusto Proiete Oct 20 '18 at 21:41

3 Answers3

12

If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog x = new OpenFileDialog();
    x.Multiselect = true;
    x.ShowDialog();
    string[] result = x.FileNames;

    foreach (string y in result)
       MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

For files and folders you need to use the CommonOpenFileDialog included with the WinAPI, the particular class is here.

JMK
  • 27,273
  • 52
  • 163
  • 280
  • 1
    You need to set the `Multiselect` property of the `OpenFileDialog` to `true` for that to work :) – Jon Grant Jul 24 '12 at 09:38
  • 2
    Did you forget about "multiple **folders** and files"? I want to select the folders, too. – Yun Jul 24 '12 at 09:40
  • I think you need to use the CommonOpenFileDialog instead, but it should work in the same way, I will update my answer shortly – JMK Jul 24 '12 at 09:51
  • 3
    CommonOpenFileDialog does not really solve that problem, because it allows either the selction of multiple folders, OR the selection of multiple files, but not both at the same time (depending on the property IsFolderPicker). However, is it possible to set the properties in a way that the dialog is more flexible, i.e. allowing the user to select both files and folders at the same time? I need that for some kind of upload-tool I am writing, and the user should be able to select whatever he thinks needs to be uploaded... – Erik May 07 '15 at 06:28
1

Try this:

openFileDialog.Multiselect = true;
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135
  • 2
    Did you forget about "multiple **folders** and files"? I want to select the folders, too. – Yun Jul 24 '12 at 09:40
  • Unfortunately, it does not work for folder. When Multiselect is true, you can attempt to select multiple folders - but when you click "Open", it does not return the selected folders, it simply navigates into the first of the selected folders, displaying files in there. Tested with filter `All files|*.*` – ToolmakerSteve Apr 01 '18 at 19:15
1

You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.

Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10
  • Thank you. Do you have any suggested third-party tool? – Yun Jul 24 '12 at 10:00
  • I have never used one...never needed to...but look at these links.. http://www.ssware.com/fldrview.htm and http://www.codeproject.com/Articles/44914/Select-file-or-folder-from-the-same-dialog – Arif Eqbal Jul 24 '12 at 10:23