Write For Us

How to select folder with modal FolderBrowserDialog?

Very common programming task is to enable user to select some folder from disc or memory card. If you are creating .Net Windows Forms application, to solve this problem you can use FolderBrowserDialog class. FolderBrowserDialog class is inherited from CommonDialog class.


FolderBrowserDialog class example

Create new Windows Forms project and place one label and one button control on form. Set button's ID to btnSelectFolder and Text property to "Select Folder...". Set label's ID property to lblSelectedFolder. Double click on button to create handler for click event, and write code like this:

[ C# ]

private void btnSelectFolder_Click(object sender, EventArgs e)
   FolderBrowserDialog fbd = new FolderBrowserDialog();
   if (fbd.ShowDialog() == DialogResult.OK)
       // User selected the folder
       lblSelectedFolder.Text = fbd.SelectedPath;
       // User canceled dialog
       lblSelectedFolder.Text = "No folder selected";

[ VB.NET ]

Private Sub btnSelectFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowseFolder.Click
   Dim fbd As New FolderBrowserDialog
   If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
       ' User selected folder
       lblSelectedFolder.Text = fbd.SelectedPath
       ' User canceled dialog
       lblSelectedFolder.Text = "No Folder Selected"
   End If
End Sub

Press F5 to start the example. If you click on button, modal dialog will appear. You can browse folders and select some folder from tree view. Press OK button to close the dialog. Label control will show full path of the selected folder.


There are few frequently used properties of FolderBrowserDialog class. You can use RootFolder property to define a starting folder. ShowNewFolderButton property defines if user is able to create new folder (default value is True). And finally, Description property defines text shown on the top of the dialog box.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   

comments powered by Disqus