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;
}
1 comments:
There is a much easier way of using the browsefolderdialog without having to reference any additional classes.
Place a textbox on a form with a button. We will call textbox "TX_PATH" and we will just call the button "button2".
here's all you need :)
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
TX_PATH.Text = folderBrowserDialog1.SelectedPath.ToString();
}
Happy Coding!
Jay Stratemeyer
Post a Comment