Folder Browser Dialog in C#

In this C# tutorial, we will walk through a complete tutorial on how to implement the Folder Browser Dialog in C#. The Folder Browser Dialog allows you to navigate through the file system and select a folder, a crucial functionality for applications dealing with file management.

The Folder Browser Dialog is a pre-built class in C# that provides an interface for users to browse folders on their computer. It is a part of the Windows Forms library, making it easier to add this functionality to your WinForms application.

For this demo, I have created a Windows form application using Visual Studio in C#.Net.

Folder Browser Dialog in C#

Here, first, I have created a Windows form application using Visual Studio and C#. Then in the form, I added a button and changed the button text and button ID. The form with the button looks like the below:

Folder Browser Dialog in C#

Now, double-click on the button to write the code on the button click event.

You need to import the namespace first like below:

using System.Windows.Forms;

Then you need to Create a new instance of the FolderBrowserDialog class like below:

 FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

Below is the complete code that will display the dialog box to the user as well as to capture the selected folder path in a message box.

namespace DotNetDemo
{
    using System.Windows.Forms;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowseFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFolder = folderBrowserDialog.SelectedPath;
                MessageBox.Show("You selected the folder: " + selectedFolder);
            }
        }
    }
}

When you run the code, it will show you the folder browse dialog box to the user like below:

Show Folder Browser Dialog in C#

Once you select the folder, you can see it will capture and display the folder path like the one below.

c# folder browser dialog example

This is how to display Folder Browser Dialog in C#.

Change Title in C# folder browser dialog

We can also easily change the title of the C# folder browser dialog by using the code below.

folderBrowserDialog.Description = "Select your project directory";

Here is the complete code.

private void btnBrowseFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "Select your project directory";

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFolder = folderBrowserDialog.SelectedPath;
                MessageBox.Show("You selected the folder: " + selectedFolder);
            }
        }

C# folder browser dialog default path

You can also easily set the C# folder browser dialog default path. You can set an initial folder that the Folder Browser Dialog will open by setting the SelectedPath property. Like, you can set the default path to C drive like below:

folderBrowserDialog.SelectedPath = "C:\\";

The complete code is like the below:

private void btnBrowseFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "Select your project directory";
            folderBrowserDialog.SelectedPath = "C:\\";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFolder = folderBrowserDialog.SelectedPath;
                MessageBox.Show("You selected the folder: " + selectedFolder);
            }
        }

Conclusion

This comprehensive tutorial explored how to effectively implement the Folder Browser Dialog in C# for your Windows Forms application. We discussed setting up your development environment, initializing the Folder Browser Dialog, and capturing the selected folder path.

You may also like: