1

I'm trying to make a File and Folder dialog in C#. (Just a warning: I will downvote ALL FolderBrowserDialog suggestions. That dialog is an abomination of nature)

Currently I'm using the CommonOpenFileDialog found in the WindowsApiCodePack, which has the property IsFolderPicker, that makes the dialog a folder picker only. But my users need to be able to select either a file or a folder.

So I'm looking for a dialog which lets me select both a file and a folder. The means doesn't matter. C#, WPF, WindowsApiCodePack, Ookii dialogs, C++, P/Invoke, Com+, Win32, VB etc...

I just need a control that let me select a file or a folder, that I can call from C#, and that has the default Windows OpenFileDialog look. Is this possible, and how?

AkselK
  • 2,563
  • 21
  • 39
  • Hmm, how would something that looks like an OpenFileDialog be able to select both files and folders? If you went to go inside a folder to select a file wouldn't it select the folder instead? – SuperPrograman Jan 30 '13 at 16:15
  • 1
    The same way a folder browser works. You double click the folder, and it navigates to that folder. You single click on a folder, click on the Open button (in the lower right corner), and it closes the dialog and returns the selected folder. – AkselK Jan 31 '13 at 08:06

1 Answers1

1

I customized the FolderBrowser dialog a couple of years ago. I made it display the input field where the user may specify a path without having to browse for it. I think that can be customized to show files as well as folders.

This is how I did the customization:

I used .NET Reflector to disassemble the code for FolderBrowserDialog. I saved the disassembled code as MyFolderBrowserDialog.

In the reflected code I found that FolderBrowserDialog calls the unmanaged windows function SHBrowseForFolder which accepts a BROWSEINFO struct as parameter. The BROWSEINFO struct has the ulFlags member which is a flags value that may be bitmasked to include the features you want to use.

In MyFolderBrowserDialog bitmasked BIF_EDITBOX (0x00000010) into the ulFlags to get the input field.

According to the documentation if you bitmask BIF_BROWSEINCLUDEFILES (0x00004000) into ulFlags it should include files as well as folders and thus solve your problem.

I haven't actually tried to get it to display files, but I think it may be worth a shot.

Hope this helps!

mortb
  • 9,361
  • 3
  • 26
  • 44
  • I see I had a mistake in my post. I wrote that I will downvote all FileBrowserDialogs, but I mean FolderBrowserDialog. I will NOT use the FolderBrowserDialog, even though I know that I can display files in it. – AkselK Jan 31 '13 at 08:42
  • 1
    Why not build your own? We built one ourselves once using a `TreeView` and `FileSystemWatcher` to keep it up to date. – mortb Jan 31 '13 at 08:49
  • Because that would be reinventing the wheel. I don't want to spend time replicating the theme of Windows classic, 7 and 8 and do all the logic again. It's just one modification to an existing control.. – AkselK Jan 31 '13 at 09:00