C# FolderBrowserDialog

FolderBrowserDialog displays a directory selection window. Once the user selects a folder, we access it from the C# source. This control from Windows Forms provides a convenient way to select folders themselves—not files—directly in the interface.

FolderBrowserDialog in Windows Forms program

Example code

Note

First, this tutorial centers around the FolderBrowserDialog control, which allows users to select a folder from the file system for use in the program. To add a FolderBrowserDialog to your Windows Forms project, please open the Toolbox by clicking on the View menu and then Toolbox. Next, double-click the FolderBrowserDialog entry. In the bottom part of your window, a FolderBrowserDialog box will be displayed. Next here, we create a Load event on the Form to display the dialog by double-clicking on the window.

This C# tutorial shows how to use the FolderBrowserDialog control in Windows Forms.

Code for FolderBrowserDialog [C#]

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1 // Will be application-specific
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    //
	    // This event handler was created by double-clicking the window in the designer.
	    // It runs on the program's startup routine.
	    //
	    DialogResult result = folderBrowserDialog1.ShowDialog();
	    if (result == DialogResult.OK)
	    {
		//
		// The user selected a folder and pressed the OK button.
		// We print the number of files found.
		//
		string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
		MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
	    }
	}
    }
}

Output On startup, it shows the dialog. You can select a folder and press OK. It will then display the number of files in the selected folder.

Calling ShowDialog on FolderBrowserDialog instance. The Form1_Load event is raised when the program starts up, and the first thing it does it open a FolderBrowserDialog. It calls the instance ShowDialog parameterless method to do this. When the ShowDialog method is called, execution stops in this method and transfers to the new dialog. When the user clicks on OK or Cancel in the dialog, control flow returns here. We test the DialogResult enumerated type for the special value DialogResult.OK.

Reading in the SelectedPath property. Inside the if-statement, we know that the user clicked on the OK button on the FolderBrowserDialog. The C# code that is inside the if-statement simply reads in all the file paths in the folder selected into a string[] array. It then displays the number of files found by counting the paths it read in. You will get a MessageBox that tells you the number of files found in the folder you selected.

Properties

Let's look at the properties available on the FolderBrowserDialog control in Windows Forms. When using Visual Studio to develop Windows Forms, it is usually easiest to directly change the properties in the Properties pane, which you can access by clicking on View and then Properties Window in Visual Studio. Next we look at the properties pane and then describe some of the options on the pane.

FolderBrowserDialog property settings

(Name) This is the variable name of the FolderBrowserDialog in your program.

Description This is the text description that appears at the top of the dialog. The dialog in this article shows "Dot Net Perls made this."

GenerateMember Not often useful.

Modifiers Not often useful.

RootFolder Where the dialog first begins: the initial folder.

SelectedPath The folder that was selected when the dialog was exited. You can test the DialogResult first and then access this property.

ShowNewFolderButton Whether the new folder button should be visible. Usually, it is acceptable to leave this enabled.

Tag Used in Windows Forms to attach data to a control. Not often useful.

OpenFileDialog

Let's consider the difference between the OpenFileDialog and the FolderBrowserDialog as shown in this article. The OpenFileDialog is meant for selecting files, and if you want to prompt your user to select a data file, use the OpenFileDialog. However, the OpenFileDialog does not allow users to easily select folders themselves. The FolderBrowserDialog is ideal for actually selecting folders themselves. This site describes the OpenFileDialog in depth.

OpenFileDialog Tutorial

MessageBox.Show

Here we note the MessageBox class and the MessageBox.Show static method in particular. The MessageBox.Show method contains many overloads and can be used to display simple alerts and informative dialogs. Unfortunately, the MessageBox.Show method does not show truly modern dialogs in appearance currently. You can find more descriptions of the MessageBox.Show method on this site.

MessageBox.Show Examples

Summary

We looked at the FolderBrowserDialog control in Windows Forms and used it to select a directory from the file system in the user interface. We noted how you can open the FolderBrowserDialog, access the user's selection from the dialog when it is closed, and change properties on the FolderBrowserDialog to influence its display and behavior in certain ways. Finally, we noted related controls in the Windows Forms system that can be used with or instead of the FolderBrowserDialog.

Windows Forms
.NET