How to use Open File Dialog to Select a Folder

Demasterpl picture Demasterpl · Feb 10, 2012 · Viewed 45.8k times · Source

Possible Duplicate:
How do you configure an OpenFileDIalog to select folders?

I'm using C# and I want to completely avoid SelectFolderDialog to select a folder. Instead, I want to use something closer to a OpenFileDialog just to select a folder.

For a more visual example, I'm looking for something close (if not exactly) like the following: http://i44.tinypic.com/x38tx1.png

enter image description here

Any ideas?

Answer

Heinzi picture Heinzi · Feb 10, 2012

The folder selection dialog of Windows Vista looks quite similar to what you want. Unfortunately, .NET's FolderBrowserDialog shows the old Windows-XP-like dialog, which you want to avoid.

To access this Vista-style dialog, you can either

  • use some third-party .NET library (e.g. Ookii.Dialogs),

  • use the relevant Windows API calls or

  • use the Windows API Code Pack:

      using Microsoft.WindowsAPICodePack.Dialogs;
    
      ...
    
      var dialog = new CommonOpenFileDialog(); 
      dialog.IsFolderPicker = true;
      CommonFileDialogResult result = dialog.ShowDialog();
    

    Note that this dialog is not available on operating systems older than Windows Vista, so be sure to check CommonFileDialog.IsPlatformSupported first.