Wednesday, August 20, 2008

Folder browser dialog in c#

First you need to set a reference to the Shell32.dll in your .NET project. In Solution Explorer right click the References entry, select Add New... and browse to the System32 folder and select Shell32.dll.

Then add the following C# code:

  
string strPath;
string strCaption = "Select a directory.";
DialogResult dlgResult;

Shell32.ShellClass shl = new Shell32.ShellClass();
Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0,
System.Reflection.Missing.Value);

if (fld == null)
{
dlgResult = DialogResult.Cancel;
}
else
{
strPath = fld.Self.Path;
dlgResult = DialogResult.OK;
}