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:
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:
Once you select the folder, you can see it will capture and display the folder path like the one below.
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:
- Disable or Hide minimize maximize and close buttons in C#.net windows form
- Generate One Time Password (OTP) in Asp.Net using C#.Net (generate OTP in c#)
- How to create a GUID in C#.Net
- How to use jquery modal popup in asp.net
- C# Folder Browser Dialog with TextBox
- How to Delete a File from a Folder in C#
- How to Delete a Folder with Subfolders and Files in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…