3

I would like to use a Folder Browse Dialog for WPF, but there does not seem to be such an essential class for WPF.

Some recommend to use System.Windows.Forms.FolderBrowserDialog but this is a really awful dialog.

I tried Ookii.Dialogs.Wpf.VistaFolderBrowserDialog:

Ookii.Dialogs.Wpf.VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = path;
dlg.ShowDialog();

but setting the SelectedPath does not set the start folder when the Dialog opens which is essential for my program.

How can I get VistaFolderBrowserDialog to open in the correct folder?

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • 1
    I don't think Microsoft has changed anything in System.Windows.Forms.FolderBrowserDialog since 90s.... It really is aweful. – thebunnyrules Jul 18 '17 at 00:34

3 Answers3

7

I cannot reproduce this. But maybe there is just some confusion here. If I do this:

        var dialog = new VistaFolderBrowserDialog();
        dialog.SelectedPath = @"C:\Data";
        dialog.ShowDialog();

It will launch in "C:" having selected the Folder "data". When pressing OK, the result is "C:\Data". However, including the backslash at the end:

        var dialog = new VistaFolderBrowserDialog();
        dialog.SelectedPath = @"C:\Data\";
        dialog.ShowDialog();

will launch the dialog within this exact folder and selecting nothing by default. When pressing "OK", the result is again "C:\Data".

DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
2

I ended up using the Codeplex project WPF Native Folder Browser:

WPFFolderBrowser.WPFFolderBrowserDialog dlg = new WPFFolderBrowserDialog();
dlg.InitialDirectory = path;
bool? pathWasSelected = dlg.ShowDialog();
string selectedPath = null;
if(pathWasSelected == true) 
    selectedPath = dlg.FileName;
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Thanks! That was super helpful. Ookii dialog was giving me a ton of troubles with reference issues. WPF's dll plugged in like a charm, – thebunnyrules Jul 17 '17 at 02:04
0

WpfFolderBrowser.WpfFolderBrowserDialog - this is the one that worked for me best!

I did one fix though: The function ErrorHelper.HResultFromWin32(int) generate System.OverflowException when pressing "Select Folder" or "Cancel" (run in .NET 4.5.1).

To fix, I put the problematic code in unchecked block:

unchecked
{
  win32ErrorCode =
      (int)((win32ErrorCode & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);
}

McNeight/WpfFolderBrowser

neflow
  • 76
  • 3