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
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
[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:
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!