Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

Example for FolderBrowserDialog in C#

Rate me:
Please Sign up or sign in to vote.
2.64/5 (44 votes)
29 Jun 20072 min read 416K   10.5K   37   9
An article discussing FolderBrowseDialog in C#

Introduction

In various cases, applications allow the user to open a single file, but also a particular folder. For instance, in an MP3 player the user can either add a single file to the play list or can choose an entire folder of MP3 files. This article describes how folders can be opened in C#.

Using the code

The example presented in this article does not contain much code. Therefore I have inserted everything in one class, as it is still easy to understand.

Required namespaces

Besides the namespaces which are used by default, it is necessary to use the IO namespace as well. Therefore the following line is added at the top of the sample file. This namespace is required to perform step 3.

C#
using System.IO;

Step 1: Additional settings

Basically, there are two additional settings available to make the dialog more customized. First, the property ShowNewFolderButton determines whether the user can create a new folder or not.
C#
this.folderBrowserDialog.ShowNewFolderButton = false;
Second, the property RootFolder defines the top level folder of the dialog, i.e. the folder which will be shown initially.
C#
this.folderBrowserDialog.RootFolder =
    System.Environment.SpecialFolder.MyComputer;

Step 2: Show the dialog

In order to present the dialog on the screen, the ShowDialog() method is used. This method returns an enumeration that is necessary to process the user input. If the user presses Open in the dialog, the return value is DialogResult.OK as in the listing below.

C#
DialogResult result=this.folderBrowserDialog.ShowDialog();
if (result==DialogResult.OK)
{
    // the code here will be executed if the user presses Open in
    // the dialog.
}

Step 3: Get the folder content

After the user presses Open, the selected folder is stored as a string in the property SelectedPath. Now it might be useful to retrieve the content of the folder. In the following snippet, each file name will be added to a listbox. In order to make use of the GetFiles method of the Directory class, the IO namespace is needed, as I mentioned above.

C#
string foldername=this.folderBrowserDialog.SelectedPath;
    foreach (string f in Directory.GetFiles(foldername))
        this.listBox1.Items.Add(f);    

Acknowledgements

First and foremost, I used MSDN to find information for writing this article. Besides that, another useful article can be found here.

Conclusion

The FolderBrowserDialog is quite useful if you want to open a particular folder. I used this, for example, to develop some smaller tools that I use for file management. It was a surprise for me how simple it is to use.

History

  • 29 June, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
_Vitor Garcia_26-Feb-13 1:10
_Vitor Garcia_26-Feb-13 1:10 
GeneralMy vote of 5 Pin
Gun Gun Febrianza10-Aug-12 1:53
Gun Gun Febrianza10-Aug-12 1:53 
GeneralMy vote of 5 Pin
developer1429-Jun-12 9:50
developer1429-Jun-12 9:50 
GeneralMy FolderBrowseDialog is opening behide the ie using silver light 2.0 Pin
vikramsc19-Nov-09 18:21
vikramsc19-Nov-09 18:21 
GeneralRunning dialog in separate thread that resulted in blank screen - here is the solution Pin
drweb8626-Aug-09 0:27
drweb8626-Aug-09 0:27 
GeneralCheckBox Pin
A. Raees31-Aug-08 23:55
A. Raees31-Aug-08 23:55 
Generalthank you! Pin
galaxy78718-Oct-07 23:39
galaxy78718-Oct-07 23:39 
GeneralLess/Useless content Pin
ADLER129-Jun-07 6:36
ADLER129-Jun-07 6:36 
GeneralRe: Less/Useless content PinPopular
ksh648-Oct-09 2:43
ksh648-Oct-09 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.