Use SelectedPath Property of the FolderBrowserDialog Class
An example of a simple code how RootFolder and SelectedPath properties interact
Introduction
This article is not of interest to advanced programmers, as it answers the following questions:
- How to use
SelectedPath
property ofFolderBrowserDialog
class? - How to open a
FolderBrowserDialog
at the selected logical drive?
But...
Background
Despite the fact that the MSDN provides a clear definition of RootFolder
and SelectedPath
properties, it cited the examples do not provide a clear view.
I'll try to fill this gap.
Using the Code
Run an application and fill comboBox1
the next items:
private void Form1_Load(object sender, EventArgs e)
{
//The first 'comboBox1' item is string a "Browse..."
comboBox1.Items.Add("Browse...");
/*Returns an array of string
* containing the names of the logical drives on the current computer.*/
foreach (var Drives in Environment.GetLogicalDrives())
{
DriveInfo DriveInf = new DriveInfo(Drives);
/*IsReady indicates whether a drive is ready.
* For example, it indicates whether a CD is in a CD drive
* or whether a removable storage device is ready for read/write operations.
* If you do not test whether a drive is ready,
* and it is not ready, querying the drive using DriveInfo
* will raise an IOException.(MSDN)*/
if (DriveInf.IsReady == true)
{
//Populate 'comboBox1' with logical drives
comboBox1.Items.Add(DriveInf.Name);
}
}
}
If you want to explore how to work a SelectedPath
property dynamically - add any subfolder to the comboBox1
.
For this:
- Select an item (any drive)
comboBox1
control. - This is will open
FolderBrowserDialog
at the item. - In the
FolderBrowserDialog
, select any subfolder from the tree file system and click <OK>.
The path of selected subfolder is added to collection itemscomboBox1
. - Select new item.
As result is opened and deployed, the FolderBrowserDialog
with the RootFolder
and any subfolders
that are beneath it will appear in the dialog box and be selectable.
NOTE:
The SelectedPath
is an absolute path that is a subfolder of RootFolder
.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//'comboBox1' selected item
string item = comboBox1.SelectedItem.ToString();
//Create new class FolderBrowserDialog
FolderBrowserDialog fbd = new FolderBrowserDialog();
//Call method 'OpenFolderBrowserDialog'
OpenFolderBrowserDialog(fbd, item);
}
private void OpenFolderBrowserDialog(FolderBrowserDialog folderBrowserDialog, string item)
{
/*Only the specified folder and any subfolders
* that are beneath it will appear in the dialog box and be selectable.
* The SelectedPath property, along with RootFolder,
* determines what the selected folder will be when the dialog box is displayed,
* as long as SelectedPath is an absolute path that is a subfolder of RootFolder
* (or more accurately, points to a subfolder of the shell
* namespace represented by RootFolder).(MSDN)*/
//Gets the path to the system special folder
//that is identified by the specified enumeration.(MSDN)*/
folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
/*If the SelectedPath property is set before showing the dialog box,
* the folder with this path will be the selected folder,
* as long as SelectedPath is set to an absolute path
* that is a subfolder of RootFolder
* (or more accurately, points to a subfolder of the shell
* namespace represented by RootFolder).(MSDN)*/
//In case
folderBrowserDialog.SelectedPath = item;
//If not necessary to crate new folder than...
folderBrowserDialog.ShowNewFolderButton = false;
try
{
DialogResult dr = folderBrowserDialog.ShowDialog();
//Open 'FolderBrowserDialog'
if (dr == DialogResult.OK)
{
/* To see how it works 'SelectedPath' property
* add any subfolder to the 'comboBox1'
* and choose it again*/
comboBox1.Items.Add(folderBrowserDialog.SelectedPath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
On the other hand:
If "Browse..." has been selected in comboBox1
control, I see no reason to suppose that someone there is a folder with the same name, SelectedPath
does not work and as a result, FolderBrowserDialog
will be opened with the RootFolder=Environment.SpecialFolder.Desktop
only!
We see that "...as long as SelectedPath is set to an absolute path that is a subfolder of RootFolder (or more accurately, points to a subfolder of the shell namespace represented by RootFolder)". (MSDN)
History
- March 23, 2016: Article published