The Folder Browser Dialog Box
The .NET framework provides a number of commonly used dialog boxes. These are designed to allow familiar methods for user input. They include font, file and colour selection and printer configuration dialogs. This article describes the folder browser.
Common Dialog Boxes
The .NET framework includes seven standardised dialog boxes that you can use for interaction with users, whilst writing minimal amounts of code. These are the common dialog boxes. They should be familiar to the user, as they are used in many Microsoft applications and custom tools created by .NET developers. The seven dialog boxes are:
- FontDialog. Allows the user to configure a font, including setting the typeface and colour, and adding effects such as bold, italics and underline.
- ColorDialog. Lets the user choose a colour using a fixed or customisable pallet.
- FolderBrowserDialog. Allows the user to browse the folders of the local file system and those of networked computers.
- OpenFileDialog. Lets the user select one or more files from the file system, with filtering options to show only specific file types.
- SaveFileDialog. Allows the user to specify the location and name for a file that is to be saved.
- PageSetupDialog. Provides configurations options for pages of printable information, including properties for the paper size, orientation and margins.
- PrintDialog. Allows the user to set printing options prior to printing their documents and data, including selecting the printer, determining the number of copies and specifying which pages to include.
In this article we'll look at the folder browser dialog box.
The folder browser dialog box is controlled by the FolderBrowserDialog class. This class is used to display a dialog box that shows a tree of file system folders, from which the user can select. The folders include directories on the local computer, removable drives and network shares. They also include libraries and special folders, such as "Documents" or "Music". The folders are automatically filtered according to the permissions held by the user.
To demonstrate FolderBrowserDialog we need a sample solution. Create a new Windows Forms project. Once the project is loaded, add a Label and a Button to the automatically generated form. Modify the properties of these controls as shown in the table below:
|Control Name||Type||Special Properties||Purpose|
|SelectFolderButton||Button||Text = "Select Folder"||Displays the folder browser dialog box when clicked.|
|FolderDetails||Label||Text = "No folder selected"||Shows the folder that the user selected with the folder browser dialog box.|
If you download the sample project using the link at the start of the article, the form layout is as follows:
Showing the Folder Browser Dialog Box
As with all of the common dialog boxes, to show the window you call the ShowDialog method. The dialog box is modal, preventing the calling code from continuing until the user clicks either the OK or Cancel button, or closes the window in another manner. The clicked button is returned as a constant from the DialogResult enumeration. You should check that the return value is OK before performing any activity based upon the user's input.
The path to the selected folder is held in the SelectedPath property of the FolderBrowserDialog class, as a string. To demonstrate, add a Click event to the button by double-clicking it. Modify the generated method as shown below:
private void SelectFolderButton_Click(object sender, EventArgs e)
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
if (dialog.ShowDialog() == DialogResult.OK)
FolderDetails.Text = dialog.SelectedPath;
The above code displays a folder browser dialog when the user clicks the button. After the user selects a folder and clicks OK, the path to the selection is displayed using the Label. Try running the program and using the dialog box to see the results. The dialog box should appear as shown below:
15 December 2013