The .NET OpenFileDialog and SaveFileDialog are fairly full-featured. They’re a lot like Windows Explorer with some added file selection capabilities thrown in.
In contrast, the FolderBrowserDialog is pretty pathetic. It only lets you use a hierarchical tree-like display, doesn’t provide different views (such as Detail, List, and Large Icon), doesn’t allow you to search, and doesn’t let you type in parts of the folder’s path if you know them.
Although Windows itself uses a much more powerful file and folder selection dialog, Microsoft hasn’t yet given it to .NET developer. Once upon a time, Microsoft provided the Windows API Code Pack, which works in Windows Vista and later. Then, for no obvious reason, they removed the Code Pack and erased all evidence of its existence. (Sort of like a weird, nerdy version of Liam Neeson’s film “Unknown.”)
Fortunately, there are several libraries available at GitHub that can help. This link leads to a version of the Windows API Code Pack 1.1 posted by jamie-pate.
To install the Code Pack, follow the link and click the Download button. Extract the files from the downloaded zip file and put them somewhere safe where you won’t need to move them later.
Launch Visual Studio, start a new project, open the Project menu, and select Add Reference. On the Add Reference dialog, and click the Browse tab. Next, browse into the folder that you unzipped, enter the “binaries” folder, and select the following two files.
- Microsoft.WindowsAPICodePack.dll
- Microsoft.WindowsAPICodePack.Shell.dll
Now the program can use the Code Pack dialogs. To make using the dialog easier, include the following using directive:
using Microsoft.WindowsAPICodePack.Dialogs;
Now you can use the CommonOpenFileDialog or CommonSaveFileDialog components to display a file or folder selection dialog. This example uses the following code to let the user select a folder.
// Let the user select a folder. private void btnSelect_Click(object sender, EventArgs e) { CommonOpenFileDialog dialog = new CommonOpenFileDialog(); dialog.InitialDirectory = txtFolder.Text; dialog.IsFolderPicker = true; if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { txtFolder.Text = dialog.FileName; } }
I once hoped that Micosoft would eventually include these dialogs with Visual Studio, or at least update the FolderBrowserDialog to something more usable. It’s been so long, however, that I think I’m going to have to let that dream die. It seems likely that we’ll be stuck using this old saved version of the Code Pack or some other dialogs written by other people for the foreseeable future.




How to implement a managed component that wraps the Browse For Folder common dialog box by using Visual C#
http://support.microsoft.com/kb/306285
This looks like the same dialog you get by using the FolderBrowserDialog.
The goal here was to use the more powerful dialog similar to Windows Explorer that Windows uses all over the place. Even Visual Studio uses that one.
Run the sample from that KB article and compare it to the dialog displayed by this example.
is this available for windows xp?
Sorry but I don’t know. It looks like Microsoft has retired these tools and I don’t know if there’s a replacement. You can probably search around and find something usable. Or with some work, you could write one yourself.
install-package winapicp
use this command in NuGet Power Manager Console to install Windows CodePack API And then By importing package “Using Microsoft.WindowsAPICodePack.Dialogs;” you can have this functionality.
Anyone know another way to get it. the archive link done not seem to work any more
one more thing will this work for asp.net web forms?
It looks like you can still get it in various archived forms. For example, see this post:
Windows API Code Pack: Where is it?
You can still use the FolderBrowserDialog, although as I mentioned in this post it’s pretty pathetic. I have no idea why Microsoft hasn’t made their folder browser available to developers.
FolderBrowserDialog may work with asp.net but I doubt the Code Pack dialog will.
I added this to my windows form, but now my form is permanently shrinking every time I access the dialog. This issue is described here:
https://stackoverflow.com/questions/42975285/commonopenfiledialog-cause-windows-form-to-shrink
Any idea why this might be happening? Or how I can prevent it?
Did you try the solution in that post about resetting the display size to 100%?
I haven’t heard of this before. If that doesn’t work, I’m not sure what you can try next.
Post a followup either way. I’d be interested in hearing whether that works.
The form still shrinks when I set the display size to 100%. It just starts smaller and shrinks more.
I figured out that this is a Windows 10 issue. ICommonOpenFileDialog causes this problem when used with Windows 10 but not with Windows 7. There is a newer version of the dialog called the Common Item Dialog that should work with Windows 10.
I can’t find a good example using the Common Item Dialog in C#. The example I see use C++. I also can’t find it used as a folder browser.
Do you have any references for those?
I used:
http://www.ookii.org/software/dialogs/
Hello, do you know if the same thing can be done in VBA ?
Thank you in advance.
Yohann
You can add references to a VBA project (use the Tools menu’s Add Reference command), but I think the DLLs in the Windows API Code Pack aren’t built in a way that lets VBA understand them.
However, you can use the msoFileDialogFolderPicker a lot more easily. See this post: VBA Select Folder with msoFileDialogFolderPicker.