Click here to Skip to main content
15,880,364 members
Articles / Programming Languages / C#

FolderBrowserDialogEx: A C# Customization of FolderBrowserDialog

Rate me:
Please Sign up or sign in to vote.
4.95/5 (15 votes)
18 Feb 2011CPOL3 min read 83.7K   4.5K   21   29
FolderBrowserDialogEx class library and a WinForms app that demos it.
In this article, you will learn about the FolderBrowserDialogEx class library and also see a WinForms app that demos it.

Introduction

We've all used FolderBrowserDialog and its precursor SHBrowseForFolder. While these common controls are very useful in stock form, they never quite seem to do exactly what you want - there's always some UI artifact that you really don't want. And, like many things in .NET, the managed API seems to be a thin wrapper around the unmanaged API, and the managed API therefore inherits all the quirks.

I had a few gripes about FolderBrowserDialog, especially:

  • The stock "Browse For Folder" dialog title... I want to say something more informative.
  • There's no option for a path edit box... it's in SHBrowseForFolder but disappeared in FolderBrowserDialog.

Background

In setting out to solve the issues, I started in the usual way: search the Web for code samples! I found two particularly useful samples:

Neither of which was the exact solution I sought, but both provided invaluable starting points. XBrowseForFolder had many of the features I wanted... but it's in C++. PInvoke's SHBrowseForFolder C# sample code provided a glimpse into what's possible in managed code, but is just a snippet. I encourage you to explore both links above as they can provide details that this sample intentionally omits.

Using the Code

XBrowseForFolder does a great job explaining the techniques for using SHBrowseForFolder, so I won't rehash that here. The one semi-messy part of doing it in .NET is the interaction between managed and unmanaged code, specifically connecting to SHBrowseForFolder's callback function. This requires Marshaling and a class - herein named InitData - to pass data from the main function to the callback. I originally tried to pass the class object itself to the callback but that would hang, thus a helper class was necessary.

The attached sample code contains a FolderBrowserDialogEx classlib and a WinForms app that demos it. The classlib's implementation is in FolderBrowserDialogEx.cs; the attendant P/Invoke code is in Win32.cs. The demo app is pretty much boilerplate. The main form, Form1, is designed to look and behave like the demo UI in XBrowseForFolder.

Invoking FolderBrowserDialogEx

The FolderBrowserDialogEx interface mimics that of FolderBrowserDialog:

C#
FolderBrowserDialogEx cfbd = new FolderBrowserDialogEx();
// These are identical to FolderBrowserDialog:
cfbd.SelectedPath = @"c:\";
cfbd.ShowNewFolderButton = true;
cfbd.RootFolder = Environment.SpecialFolder.Desktop;
// These are specific to FolderBrowserDialogEx
cfbd.Title = "Your custom title";
cfbd.ShowEditbox = true;
cfbd.StartPosition = FormStartPosition.CenterScreen;
// Show the UI like any other dialog:
if (cfbd.ShowDialog(this) == DialogResult.OK)
{
    // Retrieve value from cfbd.SelectedPath
}

Points of Interest

Overriding SHBrowseForFolder took a surprising number of P/Invoke APIs. I found that to do a proper job, we must have a good grounding in the pre-.NET world. I found it interesting that the stock FolderBrowserDialog doesn't offer the path editbox but it does offer the "Create New Folder" button. Seems to me that users would often enough want to type or paste a path so as to warrant the inclusion of the editbox.

I was interested and annoyed to find that SHBrowseForFolder's BIF_NEWDIALOGSTYLE makes it really hard to override the stock behavior. In particular, when BIF_NEWDIALOGSTYLE is applied, you absolutely cannot reposition the editbox... some code fights you and puts the editbox where it wants even when you try to put it where you want.

I found XBrowseForFolder to be a blast from the past. It hasn't been that long since unmanaged C++ was my primary environment, but a quick tour through this older code really made me appreciate how elegant C#/WinForms is compared to C++/MFC.

History

  • 18th February, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I can type "while" and "for" very quickly

Comments and Discussions

 
QuestionSHBrowseForFolder is deprecated and IFileDialog is the new API that should be used Pin
Sandy Brun16-Nov-20 9:45
Sandy Brun16-Nov-20 9:45 
QuestionCould this be used in a WPF app? Pin
Member 146136308-Jul-20 9:27
Member 146136308-Jul-20 9:27 
AnswerRe: Could this be used in a WPF app? Pin
Sandy Brun16-Nov-20 9:41
Sandy Brun16-Nov-20 9:41 
QuestionGet last SelectedPath Pin
Ionescu Ion19-Apr-18 3:20
Ionescu Ion19-Apr-18 3:20 
QuestionMy vote of 5 Pin
Behzad Sedighzadeh15-Oct-16 5:12
Behzad Sedighzadeh15-Oct-16 5:12 
Bugselected path getting wrong Pin
Varun_Kumar11-Apr-16 19:10
Varun_Kumar11-Apr-16 19:10 
QuestionCan it be resizable? Pin
ehsiung6-Mar-15 5:34
ehsiung6-Mar-15 5:34 
QuestionShowNewFolderButton - anyone figured it out? Pin
chrisf86571-Mar-15 20:24
chrisf86571-Mar-15 20:24 
AnswerRe: ShowNewFolderButton - anyone figured it out? Pin
Behzad Sedighzadeh10-Feb-17 21:24
Behzad Sedighzadeh10-Feb-17 21:24 
AnswerRe: ShowNewFolderButton - anyone figured it out? Pin
Member 1499135112-Nov-20 7:51
Member 1499135112-Nov-20 7:51 
QuestionInteract with the contents of the edit box Pin
flipdoubt29-May-11 3:43
flipdoubt29-May-11 3:43 
AnswerRe: Interact with the contents of the edit box Pin
DLChambers29-May-11 11:49
DLChambers29-May-11 11:49 
Generalmy vote is 5 Pin
KendoTM21-Feb-11 19:27
KendoTM21-Feb-11 19:27 
GeneralRe: my vote is 5 Pin
Claudio Nicora21-Feb-11 21:56
Claudio Nicora21-Feb-11 21:56 
GeneralShowNewFolderButton on FolderBrowserDialogEx does not work Pin
RudolfHenning18-Feb-11 9:23
RudolfHenning18-Feb-11 9:23 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
DLChambers18-Feb-11 10:06
DLChambers18-Feb-11 10:06 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
imgen18-Feb-11 20:28
imgen18-Feb-11 20:28 
Hi, about this, I have one implementation from DotNetZip library that supports all the features of your folder browser dialog (including the none functional new folder button) and it is resizable. You can download it from SkyDrive
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
flipdoubt31-May-11 6:43
flipdoubt31-May-11 6:43 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
imgen31-May-11 14:58
imgen31-May-11 14:58 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
cedral27-Apr-12 8:06
cedral27-Apr-12 8:06 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
John Mc Hale29-Jan-15 4:22
professionalJohn Mc Hale29-Jan-15 4:22 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
imgen29-Jan-15 15:35
imgen29-Jan-15 15:35 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
imgen29-Jan-15 16:25
imgen29-Jan-15 16:25 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
John Mc Hale29-Jan-15 18:24
professionalJohn Mc Hale29-Jan-15 18:24 
GeneralRe: ShowNewFolderButton on FolderBrowserDialogEx does not work Pin
John Mc Hale29-Jan-15 4:18
professionalJohn Mc Hale29-Jan-15 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.