Sometimes it’s necessary to allow a user to select a folder not a file. The .NET FolderBrowserDialog (part of System.Windows.Forms) is showing its age. With Vista came a new folder selection dialog based on the OpenFileDialog but the Vista-style dialog has not been added to .NET. This post includes code (c#) to display the Vista/Win 7 style folder selection dialog.
Download the source code. There’s no license as such as you are free to take and do with the code what you will.
Here’s what the old folder selection dialog looks. If you search for information about selecting folders from a .NET application, this is what you’ll be referred to.
By contrast, if you use a Windows 7 application, this is what you will see.
Much better. IMHO it’s better because you are able to sort and search plus, sometimes, the old dialog will not enumerate network computers making it impossible to select folders on networked resources. In the new-style dialog you are able to enter a clue into the address box to get it to the correct machine.
Being a Vista-style dialog box the dialog can only be shown on Vista or later (not WinNT, XP or 2003). The code gracefully reverts to show the old-style dialog if OS is not major version 6 or later.
How does it work?
It turns out that the ability to show a Vista-like dialog is present in .NET but the methods are not public. So the ShowDialog() method uses reflection to call CreateVistaDialog and pass in all the parameters. The technique to do this is taken from the Google code project FED.