How to: Browse a Folder
You can use the built-in FolderBrowserDialog component to enable users to browse a folder. To display a dialog box, you use the ShowDialog method. You can then check whether the user clicked the OK button by using the DialogResult.OK field.
To display the folder browser dialog box
On the File menu, click New Project.
The New Project dialog box appears.
Click Windows Forms Application and then click OK.
Add a Label control to the form, and use the default name, Label1.
Add a Button control to the form, and change the following properties in the Properties window:
Property
Value
Name
folderPath
Text
Path
Drag a FolderBrowserDialog component from the Dialogs tab of the Toolbox to the form.
folderBrowserDialog1 appears in the component tray.
Double-click the button to create the default event handler in the Code Editor.
In the folderPath_Click event handler, add the following code to display the folder browser dialog box and display the selected path in the label.
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { this.label1.Text = folderBrowserDialog1.SelectedPath; }
Press F5 to run the code.
When the form appears, click Path, click a folder in the list, and then click OK.
Verify that the selected path appears in the label.
Close the application.
See Also
Concepts
Using Built-in Dialog Boxes in Your Application
Designing a User Interface in Visual C#