In this tutorial, we will explore how to use the Folder Browser Dialog in C# with multi-select functionality. If you’re working on a C# project where you need to allow the user to select multiple folders at once, this tutorial is for you. Here, I will show you how to write the code that allows multi-folder selection.
C# Folder Browser Dialog with Multi-Select
Here, I am using a Windows forms application in C#. I have added a button control in the Windows form application. Onclick of that button, the C# folder browser dialog with multi-select functionality will open.
The built-in FolderBrowserDialog in C# does not natively support multi-folder selection. However, you can work around this limitation by allowing the user to select files instead and then mapping those back to their parent directories.
Double-click on the button to generate the event handler where you can write the code.
First, you need to import the following namespaces.
using System;
using System.Windows.Forms;
using System.IO;
Then, write the following code in the button click. Check out the complete code below:
namespace DotNetDemo
{
using System.Windows.Forms;
using System.IO;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowseFolder_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.ValidateNames = false;
openFileDialog.CheckFileExists = false;
openFileDialog.CheckPathExists = true;
openFileDialog.FileName = "Folder Selection.";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string[] fileNames = openFileDialog.FileNames;
// Create a HashSet to avoid duplicate folder paths
HashSet<string> folderPaths = new HashSet<string>();
foreach (string fileName in fileNames)
{
string folderPath = Path.GetDirectoryName(fileName);
folderPaths.Add(folderPath);
}
// Now folderPaths contains all the unique folder paths
foreach (string folderPath in folderPaths)
{
// Do something with each folder path
MessageBox.Show("You selected the folder: " + folderPath);
}
}
}
}
}
Here is how the complete code works:
- We use OpenFileDialog instead of FolderBrowserDialog.
- The Multiselect property is set to true to allow multiple file selection.
- ValidateNames, CheckFileExists, and CheckPathExists are set to ensure we can select folders.
- The HashSet folderPaths keeps track of unique folder paths, as selecting multiple files from the same folder should still result in one folder path being used.
Conclusion
In this C# tutorial, I have explained how to implement a multi-select folder browser dialog in C# by leveraging OpenFileDialog. Although it’s not a direct multi-folder selection, it’s a practical workaround for getting multiple folder paths. This is useful for C# projects where you need to allow the user to work with multiple folders simultaneously.
You may like:
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…