How to use Open File Dialog to Select a Folder [du

2019-01-17 07:25发布

问题:

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

Any ideas?

回答1:

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.