How to use Open File Dialog to Select a Folder [duplicate]
Asked Answered
W

1

22

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?

Wearproof answered 10/2, 2012 at 12:38 Comment(5)
The Vista IFileDialog interface exposes this option. Available in the Windows API Code Pack, CommonOpenFileDialog class, IsFolderPicker property.Ayeshaayin
@HansPassant: If you add that as an answer, I'll upvote it.Caliginous
@HansPassant: Could you give an example of this? I agree with Heinzi.Wearproof
@Demasterpl: See https://mcmap.net/q/534705/-folder-browser-dialog-like-open-file-dialog which links to this article for a working solutionItinerancy
thank you so muchhhhhhhhhhhhhhhhh you asked exactly what i was looking for! :)Barranquilla
C
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.

Caliginous answered 10/2, 2012 at 12:43 Comment(12)
Both Winforms in 3.5 and WPF in 4.0 were updated to use the Vista dialog.Ayeshaayin
@HansPassant: No. I just tried it: new FolderBrowserDialog().ShowDialog(); in a .NET 4.0 WinForms app shows the same old, ugly FolderBrowserDialog.Caliginous
Check the FileDialog.AutoUpgradeEnabled property.Ayeshaayin
@HansPassant: FolderBrowserDialog does not derive from FileDialog. Hence, it lacks this property (and its functionality).Caliginous
Ah, we're talking about different classes. Never mind.Ayeshaayin
Are there any "solid" (Simple) examples on use of the Windows API calls?Wearproof
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.Delaware
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?Electron
var dialog = New CommonOpenFileDialog(); <-- VS2013 errors out with 'New'. Change it to 'new' for it to work.Cummerbund
@CaTx: Thanks. That happens when you get too much exposure to VB...Caliginous
Thanksssssssssssssssssssssssssssssssssssssssss, this is exactly what i was looking for! and simplest one. like Visual Studio folder browsing(for selecting project path)Barranquilla
this one changes the font size in higher resolution displays like 4K.Polychrome

© 2022 - 2024 — McMap. All rights reserved.