Click here to Skip to main content
Click here to Skip to main content

Example for FolderBrowserDialog in C#

By , 29 Jun 2007
 

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.

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.
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.
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.

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.

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

About the Author

Martin Baeumer
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberVitorHugoGarcia26 Feb '13 - 1:10 
I found here what i was looking for.
fillen danke
GeneralMy vote of 5memberGun Gun Febrianza10 Aug '12 - 1:53 
Nice one Big Grin | :-D
GeneralMy vote of 5memberagusriyadi29 Jun '12 - 9:50 
Thank you. Concise and helpful!
GeneralMy FolderBrowseDialog is opening behide the ie using silver light 2.0membervikramsc19 Nov '09 - 18:21 
Hello,
 
i am developeing a web site in silverlight 2.0
 
var result = new StringBuilder();
var thread = new Thread(obj =>
{
var builder = (StringBuilder)obj;
using (var dialog = new FolderBrowserDialog())
{
// dialog.Description = SpecifyDirectory;
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (dialog.ShowDialog() == DialogResult.OK)
{
builder.Append(dialog.SelectedPath);
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start(result);
 
while (thread.IsAlive)
{
Thread.Sleep(100);
}
 
My FolderBrowserDialog is appearing behide the ie.
 
How i can open FolderBrowserDialog on my web page.
 
uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

GeneralRunning dialog in separate thread that resulted in blank screen - here is the solutionmemberCuchuk Sergey26 Aug '09 - 0:27 
If you're running FolderBrowserDialog from a winforms application in separate thread you should use the following code to make it work without erroneous or blank screen (when no folder tree visible)
 
/// <summary>
/// Gets the folder in Sta Thread
/// </summary>
/// <returns>The path to the selected folder or (if nothing selected) the empty value</returns>
private static string ChooseFolderHelper()
{
var result = new StringBuilder();
var thread = new Thread(obj =>
{
var builder = (StringBuilder)obj;
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = SpecifyDirectory;
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (dialog.ShowDialog() == DialogResult.OK)
{
builder.Append(dialog.SelectedPath);
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start(result);
 
while (thread.IsAlive)
{
Thread.Sleep(100);
}
 
return result.ToString();
}
GeneralCheckBoxmemberA. Raees31 Aug '08 - 23:55 
Can FolderBrowseDialog display checkbox...like if I m saving a file in a folder and want to display a checkbox open document....May I ???? If yes how????
Generalthank you!membergalaxy78718 Oct '07 - 23:39 
Very interest article.Smile | :)
GeneralLess/Useless contentmemberADLER129 Jun '07 - 6:36 
Sorry if I'm strict with you. But there are several aspects which make you article, also if for beginners, useless:
1. Why to write an general article on this WinForms control if the info is already on MSDN.
2. Why to set an extra variable for the SelectedPath property -> extra memory consumption.
3. the same question on the ShowDialog() returned value.
4. There are already other articles on this topic.
5. (SORRY! if my opinion is wrong but) I think you just wanted to upgrade your status here.
6. There is no need to include downloadable source code.
 
So, if you want to make a beginner article on this topic, you should at least explain all properties and methods that are usable by this control and maybe (this would make your article more informable) how this control works internally.
 
_____________________________
The force of .NET is with me!

GeneralRe: Less/Useless contentmemberksh648 Oct '09 - 2:43 
1. Why to write an general article on this WinForms control if the info is already on MSDN.
2. Why to set an extra variable for the SelectedPath property -&gt; extra memory consumption.
3. the same question on the ShowDialog() returned value.
4. There are already other articles on this topic.
5. (SORRY! if my opinion is wrong but) I think you just wanted to upgrade your status here.
6. There is no need to include downloadable source code.
 
1. This is a much less intimidating format for a novice programmer than the MSDN page. Introducing people to something in this way is nice Smile | :)
2. I would rather keep the string in memory than the dialog box. If you plan on using the path later in the application you obviously need to store it somewhere.. Storing it in the dialogbox would be um.. silly.
3. It makes fiddling about with it much easier. This is not an optimization discussion.. it is for newbies.
4. This one happens to be first one in many google searches so.. deal with it :-p
5. And the problem with this is what? The article is helpful and decently written.
6. For a newbie it is not a bad idea. And if a teacher in lower levels of education want to show students/pupils code it is much easier to get down int othe code and look at it without having to first learn how to create a project and do all the "housekeeping" required to create a new one.
 

And my final comment? Storage is cheap. If the information is not wrong what the hell is the problem with this article? We need better ways to search, not less content.
 
I realize this is an ancient comment but I hate to see it stand unchallenged because it sounds so incredibly disrespectful towards someone who put in the effort to contribute.
 
*A miffed professional engineer who dabbles in education*

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 29 Jun 2007
Article Copyright 2007 by Martin Baeumer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid