How do you configure an OpenFileDialog to select folders?
I have a dialog that I wrote called an OpenFileOrFolder dialog that allows you to open either a folder or a file.
If you set its AcceptFiles value to false, then it operates in only accept folder mode.
You can download the source from GitHub here
There is the Windows API Code Pack. It's got a lot of shell related stuff, including the CommonOpenFileDialog
class (in the Microsoft.WindowsAPICodePack.Dialogs
namespace). This is the perfect solution - the usual open dialog with only folders displayed.
Here is an example of how to use it:
CommonOpenFileDialog cofd = new CommonOpenFileDialog();
cofd.IsFolderPicker = true;
cofd.ShowDialog();
Unfortunately Microsoft no longer ships this package, but several people have unofficially uploaded binaries to NuGet. One example can be found here. This package is just the shell-specific stuff. Should you need it, the same user has several other packages which offer more functionality present in the original package.
You can use FolderBrowserDialogEx - a re-usable derivative of the built-in FolderBrowserDialog. This one allows you to type in a path, even a UNC path. You can also browse for computers or printers with it. Works just like the built-in FBD, but ... better.
(EDIT: I should have pointed out that this dialog can be set to select files or folders. )
Full Source code (one short C# module). Free. MS-Public license.
Code to use it:
var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
dlg1.Description = "Select a folder to extract to:";
dlg1.ShowNewFolderButton = true;
dlg1.ShowEditBox = true;
//dlg1.NewStyle = false;
dlg1.SelectedPath = txtExtractDirectory.Text;
dlg1.ShowFullPathInEditBox = true;
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;
// Show the FolderBrowserDialog.
DialogResult result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
txtExtractDirectory.Text = dlg1.SelectedPath;
}