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

A very simple BrowseForFolder class

Rate me:
Please Sign up or sign in to vote.
3.55/5 (19 votes)
2 Dec 20021 min read 76.4K   1.7K   22   5
A very simple class to include the browse for folder window into your C# project

Sample Image - browseforfolder.png

Introduction

There is a lot of code on the net about the browse for folder window, but when I was starting to learn C# I didn't find any really good example and description of how to use it. Someone still used the old API to call it, someone else didn't say anything about the System.Design.dll and, however, I didn't find any demo project, always only few lines of code. For a beginner this is not a true help... so, I'm posting here a simple class, a demo project and all the instructions to add the useful BrowseForFolder window into your .NET programs.

Using the code

Ok, let's start.

first of all you have to include into your project the BrowseForFolder class (File/Add Existing Item from menu and select the BrowseForFolder.cs file), then you have to include the System.Design.dll in your References (Project/Add Reference from menu).

Now you can add this line of code in the namespace declaration:
C#
using Utility.BrowseForFolder;

[Sure, this is an optional step (you can change the BrowseForFolder class's namespace to your program namespace), but I suggest you to keep it as a general code, in other words to build a collection of useful and indipendent little bricks of code.]

The last step is to add these few lines of code in your program:

C#
string myPath;

BrowseForFolderClass myFolderBrowser = new BrowseForFolderClass();
myPath = myFolderBrowser.BrowseForFolder("enter_here_your_text");

Ok, you have done it! ;-) Now into myPath you have the user selected path, otherwise if the user has pressed the Cancel button into myPath you'll have a blank field (myPath -> "").

Note: The path returned form myFolderBrowser.BrowseForFolder will always end with a "\" character.

Points of Interest

There are a lot of comments with the code, so... enjoy!

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
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA 100% .NET solution: Pin
Frans Bouma10-Dec-02 23:30
Frans Bouma10-Dec-02 23:30 
GeneralShell API solution Pin
Barrett8085-Dec-02 7:37
Barrett8085-Dec-02 7:37 
This is a weird coincidence, but last night I posted a question over at www.experts-exchange.com on this very subject. I received a nice, but skeletal, solution using the shell API. It's limited, but works for simple cases. Check it out at:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20415783.html

GeneralRe: Shell API solution Pin
Anonymous7-Feb-05 4:50
Anonymous7-Feb-05 4:50 
QuestionRisky? Pin
Michael Potter4-Dec-02 4:03
Michael Potter4-Dec-02 4:03 
AnswerRe: Risky? Pin
David Stone4-Dec-02 13:54
sitebuilderDavid Stone4-Dec-02 13:54 

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.