Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CustomFolderBrowserDialog

0.00/5 (No votes)
20 Oct 2012 1  
A custom FolderBrowserDialog with Non Special Root Folder

Introduction

In this tip, I provide you with a CustomFolderBrowserDialog which allows you to limit browsing directories to a non-special folder.

Background

A much desired feature which is locked in .NET native FolderBrowserDiaog is the capability to set RootFolder to some custom folder. Some applications need to limit the user to browse only the sub directories of a specified folder, not the whole hard drives.

The ShowCFBD Method

The CustomFolderBrowserDialog is presented as a simple method call, you just need to pass your desired parameters to this method and it will show your directory hierarchy with your desired root folder not limited to Environment.SpecialFolder enum.

ShowCFBD has the following arguments and return value:

  • rootFolder: a string representing the path of your desired RootFolder
  • title: The description title appears on top of dialog
  • selectedPath: which is filled by the path the user selects
  • returns boolean: The return value specifies if the user has pressed OK or Cancel button
public bool ShowCFBD(String rootFolder, String title, out String selectedPath)
{
    var shellType = Type.GetTypeFromProgID("Shell.Application");

    var shell = Activator.CreateInstance(shellType);

    var result = shellType.InvokeMember
    ("BrowseForFolder", BindingFlags.InvokeMethod, null, shell, new object[] 
    { 0, title, 0, rootFolder });

    if (result == null)
    {
        selectedPath = "";

        return false;
    }
    else
    {
        StringBuilder sb = new StringBuilder();

        while (result != null)
        {
            var folderName = result.GetType().InvokeMember("Title", BindingFlags.GetProperty,
                null, result, null).ToString();

            sb.Insert(0, String.Format(@"{0}\", folderName));

            result = result.GetType().InvokeMember("ParentFolder", BindingFlags.GetProperty,
                null, result, null);
        }

        selectedPath = sb.ToString();

        selectedPath = Regex.Replace(selectedPath, @"Desktop\\Computer\\.*\(\w:\)\\",
            rootFolder.Substring(0, 3));

        return true;
    }
} 

Using the Code

The code usage is very simple, just call the ShowCFBD method passing an out string path to that and checking the return boolean value.

     String selectedPath;
	
    if (ShowCFBD(@"C:\", "Please select a folder", out selectedPath)) 
    {
        MessageBox.Show("You've selected the " + selectedPath);
    } 
    else
    {
        MessageBox.Show("Please select a folder first");
    }  

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