For browsing and selecting a folder on a computer, a FolderBrowserDialog control is used. A typical FolderBrowserDialog is a control where we can see Windows Explorer-like features to navigate through folders and select a folder.
We often have to prompt users to select a folder in Windows applications we create, most often to save a set of files. The Windows Forms FolderBrowserDialog component allows us to easily accomplish this task.
To choose folders with the FolderBrowserDialog component
FolderBrowserDialog
component's DialogResult
property to see how the dialog box was closed and get the value of the FolderBrowserDialog
component's SelectedPath
property.RootFolder
property, which takes a member of the Environment. SpecialFolder
enumeration.Description
property, which specifies the text string that appears at the top of the folder-browser tree view.
Example:
In the example below, the FolderBrowserDialog
component is used to select a folder, similar to when we create a project in Visual Studio and a folder to be saved in is prompted to select. The name of the folder will then be displayed on the form in a TextBox control. Placing the location in an editable area, such as a TextBox control, is a good idea to allow users to edit their selection in case of error or other problems.
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result==DialogResult.OK)
{
txtFolder.Text = folderBrowserDialog1.SelectedPath;
}
}
Complete Code :
using System;
using System.Windows.Forms;
namespace FolderBrowserDialogControl
{
public partial class frmFolderDialog : Form
{
public frmFolderDialog()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result==DialogResult.OK)
{
txtFolder.Text = folderBrowserDialog1.SelectedPath;
}
}
}
}
Folder Dialog Control Source Code