ARTICLE

FolderBrowserDialog in C#

Posted by Mahesh Chand Articles | Windows Controls C# June 28, 2010
A FolderBrowserDialog control is used to browse and select a folder on a computer.
Reader Level:


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

Creating a FolderBrowserDialog

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 and not 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 following two lines of code.

 

private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;

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, set its properties and add FolderBrowserDialog class to the Form controls.

First step to create a dynamic FolderBrowserDialog is to create an instance of FolderBrowserDialog class. The following code snippet creates an FolderBrowserDialog control object.

FolderBrowserDialog folderDlg = new FolderBrowserDialog();

 

ShowDialog method displays the FolderBrowserDialog.

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 where the browsing starts from.

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.

private void BrowseFolderButton_Click(object sender, EventArgs e)

{

    FolderBrowserDialog folderDlg = new FolderBrowserDialog();

    folderDlg.ShowNewFolderButton = true;

    // Show the FolderBrowserDialog.

    DialogResult result = folderDlg.ShowDialog();

    if (result == DialogResult.OK)

    {

        textBox1.Text = folderDlg.SelectedPath;

        Environment.SpecialFolder root = folderDlg.RootFolder;

    }

}

 

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.

Login to add your contents and source code to this article
post comment
     

Does anyone know where I can get a folderBrowserDialog with multi folders select capability? Thanks...

Posted by John Tran Jun 01, 2012

Thanks guys. Harsh, I have written a Silverlight article too. Search this site or go to Silverlight category from header > Technologies.

Posted by Mahesh Chand May 30, 2012

Thanks a lot - I had initially thought I could access this from a property in OpenFileDialog().

Posted by John Chagbert May 22, 2012

Thanks a lot - I had initially thought I could access this from a property in OpenFileDialog().

Posted by John Chagbert May 22, 2012

Hi this is tssrajkumar Its really helps me Thank you so much.. keep it on..

Posted by Raj kumar May 10, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.