Use a standard Windows dialog to let the user select a folder in C#


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 lets you use only 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. Fortunately the Windows API Code Pack, which works in Windows Vista and later, makes using that dialog easy.

First go to archive.msdn.microsoft.com/WindowsAPICodePack. Download and unpackage the code pack. Save its contents in some reasonably development-oriented location such as C:\Windows\Microsoft.NET or wherever you have Visual Studio installed.

Next in your program add references to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll.

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;
}
}

Hopefully the .NET Framework will eventually include these dialogs, or at least update the FolderBrowserDialog to something more usable. Until then, you can use the Windows API Code Pack’s versions.

   

This entry was posted in API, controls, files. Bookmark the permalink.

13 Responses to Use a standard Windows dialog to let the user select a folder in C#

  1. Annon says:

    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

  2. Rod Stephens says:

    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.

  3. naftaly weinberger says:

    is this available for windows xp?

    • RodStephens says:

      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.

      • Unknown says:

        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.

  4. phil sanders says:

    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?

    • RodStephens says:

      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.

  5. Dylan A says:

    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?

    • RodStephens says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *