9

I have researched a lot to find a suitable answer to this problem, but I am failing .

I can see multiple questions asked here and on other forums also , but no clear answer that brings a clear solution .

I want OpenFileDialog to select file/files for me as well as allow me to select folders also. eg. a. either Multiple files OR multiple folders (- most Prior) b. combination of Files and Folders (-less Prior)

I figured our similar question here ( so please don't mark it as duplicate )

Question 1 [Answer links are broken]

Question 2 [Question isn't completely asking what my requirements are.]

Please guide me through some solution . I am a novice and a learner.

Any help or pointers would be very helpful .

Thanks.

Community
  • 1
  • 1
B Bhatnagar
  • 1,706
  • 4
  • 22
  • 35
  • it's confusing that what if **user double-clicks a folder**? normally if we can select the folder, that folder will be added into the selected list, but the folder shouldn't be opened. While the behavior we want is opening the folder. – King King Sep 17 '13 at 05:27
  • There is no built-in solution for this. As far as I'm aware of your only option is to design your own solution. You can try extending either `OpenFileDialog` or `FolderBrowserDialog` but not sure if that will work well. You'll probably be better off extending from `CommonDialog`. – awudoin Sep 17 '13 at 19:31
  • @awudoin: `OpenFileDialog` or `FolderBrowserDialog` class cannot be inherited as per MSDN ( [doc1](http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx) & [doc2](http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx) ) . Indeed Exetending from `CommonDialog` is again my last option alive. Thanks :) – B Bhatnagar Sep 20 '13 at 06:20

4 Answers4

3

You can't select folder with OpenFileDialog as well as you can't select files with FolderBrowserDialog. But there is an open source control for .net which allows you to select both files and folders you can check it here : OpenFileOrFolderDialog

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
3
var dialog = new OpenFileDialog();
dialog.ValidateNames = false;
dialog.CheckFileExists = false;
dialog.CheckPathExists = true;
dialog.FileName = "Must set default";
dialog.ShowDialog() // will allow both files and folders to be selected

Quite hack-ey.

Source

  • This allows you to select a non-existing default name, indicating the folder. Excellent! +1 – Roland Feb 25 '22 at 11:13
2

OpenFileDialog is used to open a file not folder

To allow selection of multiple files set Multiselect property to true.

For selecting Folder it's mentioned in the docs

If you want to give the user the ability to select a folder instead of a file, use FolderBrowserDialog.

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • 8
    This **doesn't** solve the OP's problem, he wants to put all the selected `folders and filenames` into `FileNames` or by someway allow user to select both `folders and files` in `OpenFileDialog`. All the things mentioned in this answer **seem to be known by the OP**, he just wants to find some hack to make it work the way he wants. – King King Sep 17 '13 at 05:53
  • @KingKing hmm..yes and there's no hack to do it..the only workaround would be develop or find 1 – Anirudha Sep 17 '13 at 06:13
0

You can create selctor "files or directories" and open standart OpenFileDialog or FolderBrowserDialog depending on user selection. Or you can create(or find) your custom file manager with options for selecting folders and files together.

Frank59
  • 3,141
  • 4
  • 31
  • 53
  • I am already using such a solution, But I want to reduce User clicks and provide some functionality right on OpenFileDialog. Thanks for your reply though. :) – B Bhatnagar Sep 17 '13 at 06:39