How to Browse Folders using Dot NET

Most of you may have come up with scenarios where you wish to let the user browse for a specific folder or file. This is most commonly occured when you are requested to open a specific file in Word or Power Point.
I would be using Visual Studio 2005 and DOT NET Framework 2.0

DOT NET 2.0 Framework supports several useful dialogs. In this, we will use the "FolderBrowseDialog" class for the example.

It can be very easily found in the "ToolBox" under "Dialogs" category. Simply drag and drop one on to your Form. Add a Button also for the demo.



Then, double click on the Search Button and it would take you to the Click Event of the Button.

Inside that, place the following LOC.




DialogResult result; result = folderBrowserDialog1.ShowDialog();

if (result == DialogResult.OK)

{


MessageBox.Show("Selected Path :" +folderBrowserDialog1.SelectedPath);
javascript:void(0)

}





Lets analyse each of the LOC.

DOT NET uses a Class named DialogResult. It can be used to access the result of various types of Dialog Boxes.

NOTE: There are different types of Dialogs available such as "OpenFileDialog", "SaveFileDialog".

The DialogResult can be used to get the type of result entered by the user. Such as pressing the "OK" button, Pressing the "Yes" button etc.

Then we display the folderBrowserDialog1 and set its result to the DialogResult reference. Then, we check for the event which is triggered by the user. In the "if" clause, we check whether the user clicked "OK" button. In that case, we would simply display the selected path in a Message Box. We get the selected path of the folder using the folderBrowserDialog1 ' s SelectedPath property.








Comments

LogicNP said…
FolderBrowserDialog is good but has some frustrating limitations. For more functionality and flexibility, you can consider using a custom control like the FolderView (http://www.ssware.com/fldrview.htm).

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline