Apr
29
2013

C#.Net How To: Use FolderBrowserDialog in C#

Folderbrowserdialog in c# is a very useful .net control. If you want to learn a way to provide a user to selct a specific folder, browse and/or create new folder then you are at right place. C#.Net provides you a class named as FolderBrowserDialog that prompts user a dialog box to select, browse and create a new folder. Lets learn what is FoldeBrowserDialog in c# and how to use it in our application.

What is Folder Browser Dialog?

FolderBrowserDialog in C#.Net
Graphical representation of FolderBrowserDialog in C#.Net

Folder Browser Dialog is a .Net control that prompts user to browse and select a folder location. FolderBrowserDialog moreover looks like a Windows Explorer that gives us a feature to navigate to different folder locations. Using FolderBroserDialog user can only select folders and not files.

FolderBrowserDialog is a class inherited from System.Windows.Forms namespace and cannot be further inherited. Folder Browser Dialog is modal box hence a user can not access other part of the applicaton when folderBrowserDialog is shown.

How to use FolderBrowserDialog in C#?

C# FolderBrowserDialog is a control in C#.Net. You can use FolderBrowserDialog in C# in the same way as you can use any other .Net controls with C#. Generally, once instantiated this control; you need to set RootFolder property of Folder browse dialog to point to a location from which it would initially start folder browsing.

Like any other controls in c#, we can use this C# FolderBrowserDialog in two ways. We can use this control in Design time Mode and/or we can use it at Code behind. In this article we are going to focus on Code behind implementation.

FolderBrowserDialog in Design Time Mode

After a creation of Form, you can always drag and drop a FolderBrowserDialog control from toolbox to windows form. After adding this C# control in your windows form, it will show on the bottom of the windows form. Please refer below screenshot.

C# FolderBrowserDialog in Design Mode
C# FolderBrowserDialog in Design Mode

FolderBrowserDialog in Code behind

Following steps would explain how to use FolderBrowserDialog in Code Behind. In this example, We would create a new Windows Forms application containing a FolderBroserDialog, List and button controls. On clicking a browse button we would be able to browse a folder location with the help of folder browser dialog in c# and would print a list of all available files and folders in List control.

  1. Create a new Windows Forms Application. Name this project as “a1ashiishFolderBrowserDialog”, select folder location and click on OK button.

    New Windows forms Application
    New Windows forms Application

  2. For the ease of demonstration, I renamed “Program.cs” to “FBDialog.cs” and “Form1.cs” to “MainForm.cs” in solution explorer. In addition I have also set the “Text” property of Windows Form to “a1ashiish-csharp.blogspot.com – FolderBrowserDialog Example”.
  3. Now put controls on windows form as given in below screenshot.
    C# Folder Browser Dialog
    C# Folder Browser Dialog

    Following conrtols are used –

    1. Browse Button : Click on this button to open a FileBrowserDialog.
    2. TextBox1 : A folder location selected using Browse button will display in this textbox. Set the Enabled property to false.
    3. ListBox1 : Shows the list of all files available in a folder location browsed through Browse button.
    4. Show Files Button : Once a folder location is selected, then clicking on this button will show all files in ListBox1.

  4. Double click on Browse button to open up a MainForm.cs and write following code.
       private void button1_Click(object sender, EventArgs e)
       {
          // Create a new instance of FolderBrowserDialog.
          FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
          // A new folder button will display in FolderBrowserDialog.
          folderBrowserDlg.ShowNewFolderButton = true;
          //Show FolderBrowserDialog
          DialogResult dlgResult = folderBrowserDlg.ShowDialog();
          if (dlgResult.Equals(DialogResult.OK))
          {
              //Show selected folder path in textbox1.
              textBox1.Text = folderBrowserDlg.SelectedPath;
              //Browsing start from root folder.
              Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
          }
       }

  5. Again go back to MainForm in Design mode and double click on “Show Files” button to open MainForm.cs file. In 4th step we have created an instance of FileBrowserDialog. Now in this step we are going to write a code to display all files and folder names in listbox. Write following code in MainForm.cs file –
       private void button2_Click(objectsender, EventArgs e)
       {
          if (!textBox1.Text.Equals(String.Empty))
          {
              if (System.IO.Directory.GetFiles(textBox1.Text).Length > 0)
              {
                  foreach (string file in System.IO.Directory.GetFiles(textBox1.Text))
                  {
                      //Add file in ListBox.
                      listBox1.Items.Add(file);
                  }
              }
              else
              {
                  listBox1.Items.Add(String.Format(“No files Found at location : {0}”, textBox1.Text));
              }
          }
       }

  6. Save all files and code to browse folder location using C# FolderBrowserDialog is ready to test the functionality of Folder Browse Dialog. Execute the application and see the output as given below –
    FolderBrowserDialog showing all files and folders
    FolderBrowserDialog showing all files and folders

In this c# tutorial we learned how to use FolderBrowserDialog in c#. If you find the information is worth, then please share the article on your social media because sharing is caring.

More articles –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

2 Comments+ Add Comment

  • Missed a space in this line:

    Environment.SpecialFolderrootFolder = folderBrowserDlg.RootFolder;

    Should be:
    Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;

    • Hi Bob,
      Thanks for noticing. I have corrected the typo. :)

Leave a comment