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

What is Folder Browser Dialog?,C#.Net How To: Use FolderBrowserDialog in C#,FolderBrowserDialog,FolderBrowserDialog Example
Share it:

What is Folder Browser Dialog?

Folder Browser Dialog is a .Net control that prompts the 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. 

FolderBrowserDialog is a class inherited from System.Windows.Forms namespace and cannot be further inherited.

In this article, we are going to see how we can use FolderBrowserDialog in C#. Like any other controls in c#, we can use this C# FolderBrowserDialog in two ways. 

The one is Design time and the other way is Run time. We are going to focus on runtime implementation.

(A) Design time – After the creation of Form, you can always drag and drop a FolderBrowserDialog control from toolbox to windows form. After adding this C# control, the will show on the bottom of the windows form. Please refer below screenshot.

C# FolderBrowserDialog in Design  Mode

C# FolderBrowserDialog in Design Mode.

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



C# FolderBrowserDialog – Create a new Windows Forms Project

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 FolderBrowserDialog Example”

3) Now put controls on windows form as given in below screenshot –

C# FolderBrowserDialog – C# Controls


In this example, I have taken the following controls –

i)  Browse Button: Click on this button to open a FileBrowserDialog.

ii) TextBox1: A folder location selected using the Browse button will display in this textbox. Set the Enabled property to false.
iii) Show Files Button: Once a folder location is selected, then clicking on this button will show all files in ListBox1.
iv) ListBox1: Shows the list of all files available in a folder location browsed through the Browse button.

4) Double click on the Browse button to open up a MainForm.cs and write the 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 the “Show Files” button to open MainForm.cs file again. In the 4th step, we have created an instance of FileBrowserDialog. Now in this step, we are going to add all files and folders in Listbox. Write the following code in MainForm.cs file –

  private void button2_Click(object sender, 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) Now code is ready to test the functionality of Folder Browse Dialog. Execute the application and see the output as given below –

C# FolderBrowserDialog – Output
Do you know:-


If you like the tutorial, then please share this tutorial with your friends on social media.
Share it:

C#

Windows Forms

Post A Comment:

0 comments: