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

[select folders]

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.


Download Example   Follow me on Twitter   RSS feed   Donate




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

13 Responses to Use a standard Windows dialog to let the user select folders 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.