Home Search Contact us About us
Title Folder Browser
Summary Simple wrapper class to expose the Design FolderBrowser. Useful when prompting the user to browse for a folder.
Contributor John McTainsh
Published 8-Nov-2002
Last updated 8-Nov-2002
Page rating   45% for 4 votes Useless Brilliant

Introduction

Browsing for a folder, not a file, is not directly provided where you might expect to find it in the System.Windows.Forms namespace. This article show where to find it and how to use it.

Where is it?

FolderBrowser resides as a protected member of the FolderNameEditor class in the System.Windows.Forms.Design namespace. To use it we will subclass FolderNameEditor.

How to use it

Firstly we need to add the System.Design.dll file to your project references. From the Menu select Project->Add Reference. Browse to the file which will probably be located in C:\WINNT\Microsoft.NET\Framework\v1.0.3705\ or simular.

Next add the following class and reference the Design namespace as follows. This can be in a new file or within an existing one.

using System.Windows.Forms.Design;

...

/// <summary>
/// Class to allow access to the Folder Browser.
/// </summary>
public class OpenFolderDialog : FolderNameEditor
{
    /// <summary>
    /// Access to a Folder browser which is hidden in the FolderNameEditor
    /// </summary>
    private FolderBrowser m_frmBrowser;

    /// <summary>
    /// Constructor, creating the folder object to use
    /// </summary>
    public OpenFolderDialog()
    {
        m_frmBrowser = new FolderNameEditor.FolderBrowser();
    }

    /// <summary>
    /// Read only access to the path that was browsed to
    /// </summary>
    public string Path
    {
        get{return m_frmBrowser.DirectoryPath;}
    }

    /// <summary>
    /// Show the dialog to allow the user to make a selection. If a selection is made
    /// it is returned in 'Path' property
    /// </summary>
    /// <param name="Title">Title to appear at the top of the dialog</param>
    /// <returns>OK if the user made a selection</returns>
    public DialogResult ShowDialog(string Title)
    {
        m_frmBrowser.Style = FolderBrowserStyles.BrowseForComputer;
        m_frmBrowser.Description = Title;
        return m_frmBrowser.ShowDialog();
    }
}

Now use it as follows. Here we will browse for a path and display it in a message box

OpenFolderDialog ofd = new OpenFolderDialog();
if( ofd.ShowDialog( "My title" ) == DialogResult.OK )
    MessageBox.Show( ofd.Path );

Conclusion

FolderBrowser provides basic functionality but if you are looking to present the path or more control then you will need to use SHBrowseForFolder.

Comments Date
I Needed it! 4-Aug-2004 syaki0156
VERY NICE INDEED! I `m working on an archiving project. I think this one helps avoiding the usage of direct Win API functions. Are there more?
Home Search Contact us About us