21

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?

Community
  • 1
  • 1
Demasterpl
  • 2,103
  • 5
  • 24
  • 32
  • 3
    The Vista IFileDialog interface exposes this option. Available in the Windows API Code Pack, CommonOpenFileDialog class, IsFolderPicker property. – Hans Passant Feb 10 '12 at 13:01
  • @HansPassant: If you add that as an answer, I'll upvote it. – Heinzi Feb 10 '12 at 13:03
  • @HansPassant: Could you give an example of this? I agree with Heinzi. – Demasterpl Feb 10 '12 at 15:09
  • @Demasterpl: See http://stackoverflow.com/a/15456640/117870 which links to [this article](http://www.lyquidity.com/devblog/?p=136 ".NET Win 7-style folder select dialog") for a working solution – Alex Essilfie Jun 09 '13 at 15:11
  • thank you so muchhhhhhhhhhhhhhhhh you asked exactly what i was looking for! :) – ACE Jul 05 '16 at 20:14

1 Answers1

30

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.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 2
    Both Winforms in 3.5 and WPF in 4.0 were updated to use the Vista dialog. – Hans Passant Feb 10 '12 at 12:57
  • @HansPassant: No. I just tried it: `new FolderBrowserDialog().ShowDialog();` in a .NET 4.0 WinForms app shows the same old, ugly FolderBrowserDialog. – Heinzi Feb 10 '12 at 13:01
  • Check the FileDialog.AutoUpgradeEnabled property. – Hans Passant Feb 10 '12 at 13:09
  • 1
    @HansPassant: `FolderBrowserDialog` does not derive from `FileDialog`. Hence, it lacks this property (and its functionality). – Heinzi Feb 10 '12 at 13:24
  • Ah, we're talking about different classes. Never mind. – Hans Passant Feb 10 '12 at 13:30
  • Are there any "solid" (Simple) examples on use of the Windows API calls? – Demasterpl Feb 10 '12 at 19:03
  • 4
    I wish I could post an example, but the question has been closed. In my opinion, this is a totally different question than the possible duplicate listed above. – Cesar Jan 02 '13 at 21:06
  • Can this be used to select multiple files AND folders at the same time? I would like to be able to either select one or multiple files, one or multiple folders, or both at the same time. What are the correct parameters? – Erik May 07 '15 at 06:03
  • var dialog = New CommonOpenFileDialog(); <-- VS2013 errors out with 'New'. Change it to 'new' for it to work. – CaTx Feb 06 '16 at 17:50
  • @CaTx: Thanks. That happens when you get too much exposure to VB... – Heinzi Feb 06 '16 at 21:23
  • Thanksssssssssssssssssssssssssssssssssssssssss, this is exactly what i was looking for! and simplest one. like Visual Studio folder browsing(for selecting project path) – ACE Jul 05 '16 at 20:16
  • this one changes the font size in higher resolution displays like 4K. – aadi1295 Jul 17 '20 at 09:29