FolderBrowserDialog In C#

A FolderBrowserDialog control is used to browse and select a folder on a computer. A typical FolderBrowserDialog looks like Figure 1 where you can see Windows Explorer-like features to navigate through folders and select a folder.
 
FBDImg1.jpg
Figure 1

FolderBrowserDialog Control

We can create a FolderBrowserDialog control using a Forms designer at design-time or using the FolderBrowserDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a FolderBrowserDialog does not have or need visual properties like others.

Design-time

To create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a FolderBrowserDialog on a Form, the FolderBrowserDialog looks like Figure 2.

FBDImg2.jpg
Figure 2

Adding a FolderBrowserDialog to a Form adds the following two lines of code. 
  1. private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;  
  2. this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();  
Run-time

Creating a FolderBrowserDialog control at run-time is merely a work of creating an instance of FolderBrowserDialog class, setting its properties and adding FolderBrowserDialog class to the Form controls.

The first step to creating a dynamic FolderBrowserDialog is to create an instance of the FolderBrowserDialog class. The following code snippet creates a FolderBrowserDialog control object.
  1. FolderBrowserDialog folderDlg = new FolderBrowserDialog();  
ShowDialog method displays the FolderBrowserDialog.
  1. DialogResult result = folderDlg.ShowDialog();  
Once the ShowDialog method is called, you can browse and select a file.

FolderBrowserDialog Properties

SelectedPath property represents the selected path in a FolderBrowserDialog control.

RootFolder property represents the root folder from where the browsing starts.

ShowNewFolderButton property represents a value indicating whether the New Folder button appears in the folder browser dialog box.
The following code snippet shows how to use a FolderBrowserDialog control and its properties.
  1. privatevoid BrowseFolderButton_Click(object sender, EventArgs e) {  
  2.     FolderBrowserDialog folderDlg = newFolderBrowserDialog();  
  3.     folderDlg.ShowNewFolderButton = true;  
  4.     // Show the FolderBrowserDialog.  
  5.     DialogResult result = folderDlg.ShowDialog();  
  6.     if (result == DialogResult.OK) {  
  7.         textBox1.Text = folderDlg.SelectedPath;  
  8.         Environment.SpecialFolder root = folderDlg.RootFolder;  
  9.     }  
  10. }  
Summary

A FolderBrowserDialog control allows users to launch Windows Folder Browser Dialog and let users select a folder. In this article, we discussed how to use a Windows Folder Browser Dialog and set its properties in a Windows Forms application.



Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.