decasteljau
decasteljau

Reputation: 8043

using OpenFileDialog for directory, not FolderBrowserDialog

I want to have a Folder browser in my application, but I don't want to use the FolderBrowserDialog. (For several reasons, such as it's painful to use)

I want to use the standard OpenFileDialog, but modified for the directories.

As an example, µTorrent has a nice implementation of it (Preferences/Directories/Put new downloads in:). The standard Open File Dialog enable the user to:

Does anybody know how to implement this? In C#.

Upvotes: 15

Views: 20773

Answers (3)

Koray
Koray

Reputation: 1806

WindowsAPICodePack

var dlg = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
dlg.IsFolderPicker = true;

Upvotes: 1

Alex Essilfie
Alex Essilfie

Reputation: 12613

See this answer by leetNightShade for 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.

Upvotes: 0

Josip Medved
Josip Medved

Reputation: 3711

I am not sure about uTorrent but this sounds pretty much like new Vista's IFileDialog with FOS_PICKFOLDERS option set. Generic C# code for it would go something like:

var frm = (IFileDialog)(new FileOpenDialogRCW());
uint options;
frm.GetOptions(out options);
options |= FOS_PICKFOLDERS;
frm.SetOptions(options);

if (frm.Show(owner.Handle) == S_OK) {
    IShellItem shellItem;
    frm.GetResult(out shellItem);
    IntPtr pszString;
    shellItem.GetDisplayName(SIGDN_FILESYSPATH, out pszString);
    this.Folder = Marshal.PtrToStringAuto(pszString);
}

Full code can be found here.

Upvotes: 5

Related Questions