Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • paste full paths in the text field at bottom
  • use "Favorite Links" bar on Vista
  • use Search on Vista
  • auto remember last directory
  • more...

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

like image 965
decasteljau Avatar asked Mar 01 '09 19:03

decasteljau


2 Answers

WindowsAPICodePack

var dlg = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
dlg.IsFolderPicker = true;
like image 112
Koray Avatar answered Nov 06 '22 21:11

Koray


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.

like image 44
Josip Medved Avatar answered Nov 06 '22 21:11

Josip Medved