27

In a C# .NET 3.5 app (a mix of WinForms and WPF) I want to let the user select a folder to import a load of data from. At the moment, it's using System.Windows.Forms.FolderBrowserDialog but that's a bit lame. Mainly because you can't type the path into it (so you need to map a network drive, instead of typing a UNC path).

I'd like something more like the System.Windows.Forms.OpenFileDialog, but for folders instead of files.

What can I use instead? A WinForms or WPF solution is fine, but I'd prefer not to PInvoke into the Windows API if I can avoid it.

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
Wilka
  • 28,701
  • 14
  • 75
  • 97

4 Answers4

36

Don't create it yourself! It's been done. You can use FolderBrowserDialogEx - a re-usable derivative of the built-in FolderBrowserDialog. This one allows you to type in a path, even a UNC path. You can also browse for computers or printers with it. Works just like the built-in FBD, but ... better.

Full Source code. Free. MS-Public license.

FolderBrowserDialogEx

Code to use it:

var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
dlg1.Description = "Select a folder to extract to:";
dlg1.ShowNewFolderButton = true;
dlg1.ShowEditBox = true;
//dlg1.NewStyle = false;
dlg1.SelectedPath = txtExtractDirectory.Text;
dlg1.ShowFullPathInEditBox = true;
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;

// Show the FolderBrowserDialog.
DialogResult result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
    txtExtractDirectory.Text = dlg1.SelectedPath;
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 6
    Isn't that the usual excuse? ;-) – Joey Dec 17 '09 at 14:43
  • This still requires use of WinForms. Not sure if this would work for an XBAP. – jpierson Mar 17 '11 at 03:12
  • 1
    You are the man! I was able to paste the entire source code from that page you linked into a blank code file and also paste your own code into my button click event (modifying to match my textbox name, of course). Couldn't have been easier! – oscilatingcretin Apr 28 '11 at 23:01
  • 2
    btw, I notice that setting the path to anything in a user folder isn't honored when the dialog opens. For example, if I go like: dlg1.SelectedPath = C:\Users\m00gs\Pictures\ and then call .ShowDialog(), I starts at the folder specified for .RootFolder. My guess is because this is a user folder. Any way to get around this? – oscilatingcretin Apr 29 '11 at 04:11
  • Little note: Ms-Pl is incompatible with the GPL. Be careful. – Cole Tobin May 10 '13 at 02:44
1

Unfortunately there are no dialogs other than FolderBrowserDialog for folder selection. You need to create this dialog yourself or use PInvoke.

aku
  • 122,288
  • 32
  • 173
  • 203
0

After hours of searching for a similar solution I found this answer by leetNightShade to a working solution.

There are three things I believe make this solution much better than all the others.

  1. It is simple to use. It only requires you include two files (which can be combined to one anyway) in your project.
  2. It falls back to the standard FolderBrowserDialog when used on XP or older systems.
  3. The author grants permission to use the code for any purpose you deem fit.

    There’s no license as such as you are free to take and do with the code what you will.

Download the code here.

Community
  • 1
  • 1
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
0

So far, based on the lack of responses to my identical question, I'd assume the answer is to roll your own dialog from scratch.

I've seen things here and there about subclassing the common dialogs from VB6 and I think this might be part of the solution, but I've never seen anything about modifying what the dialog thinks it's selecting. It'd be possible through .NET via PInvoke and some other tricks, but I have yet to see code that does it.

I know it's possible and it's not Vista-specific because Visual Studio has done it since VS 2003.

Here's hoping someone answers either yours or mine!

Community
  • 1
  • 1
OwenP
  • 24,950
  • 13
  • 65
  • 102