Share via


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

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. Add a Label control to the form, and use the default name, Label1.

  4. Add a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    folderPath

    Text

    Path

  5. Drag a FolderBrowserDialog component from the Dialogs tab of the Toolbox to the form.

    folderBrowserDialog1 appears in the component tray.

  6. Double-click the button to create the default event handler in the Code Editor.

  7. 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;
    }
    
  8. Press F5 to run the code.

  9. When the form appears, click Path, click a folder in the list, and then click OK.

  10. Verify that the selected path appears in the label.

  11. Close the application.

See Also

Concepts

Using Built-in Dialog Boxes in Your Application

Designing a User Interface in Visual C#

Other Resources

Dialog Boxes (Visual C#)

Visual C# Guided Tour