Search Amazon:

Browse for folders using Shell32.dll's BrowseForFolder method in C#

To browse for a folder in .NET you can use the OpenFileDialog but if you want to limit your users to browsing to a folder instead of a file you can use the BrowseForFolder method from the ShellClass class in Shell32.dll. This is similar to my DirectoryBrowser VB6 sample.

First you need to set a reference to the Shell32.dll in your .NET project. In Solution Explorer right click the References entry, select Add New... and browse to the System32 folder and select Shell32.dll.

Then add the following C# code:

    
    string strPath;
    string strCaption = "Select a directory.";
    DialogResult dlgResult;

    Shell32.ShellClass shl = new Shell32.ShellClass();
    Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0, 
                System.Reflection.Missing.Value);

    if (fld == null)
    {
        dlgResult =  DialogResult.Cancel;
    }
    else 
    {
        strPath = fld.Self.Path;
        dlgResult = DialogResult.OK;
    }
Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Be the first to add a comment! Items to Show:     

     
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms