[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

[Use a standard Windows dialog to let the user select folders 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 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.