2012-02-10 178 views
20

可能重複:
How do you configure an OpenFileDIalog to select folders?如何使用打開文件對話框,選擇文件夾

我使用C#,我想完全避免SelectFolderDialog選擇一個文件夾。相反,我想使用更接近OpenFileDialog的東西來選擇一個文件夾。

對於一個更直觀的例子,我在找一個接近(如果不完全)像下面這樣:http://i44.tinypic.com/x38tx1.png

enter image description here

任何想法?

+3

Vista IFileDialog接口公開此選項。在Windows API代碼包,CommonOpenFileDialog類,IsFolderPicker屬性中提供。 – 2012-02-10 13:01:02

+0

@HansPassant:如果你將其作爲答案添加,我會讚揚它。 – Heinzi 2012-02-10 13:03:31

+0

@HansPassant:你能舉個例子嗎?我同意Heinzi的觀點。 – Demasterpl 2012-02-10 15:09:36

回答

28

Windows Vista的文件夾選擇對話框看起來與您想要的非常相似。不幸的是,.NET的FolderBrowserDialog顯示了您想要避免的舊式Windows-XP式對話框。

打開Vista風格的對話框,您可以

  • 使用一些第三方.NET庫(例如Ookii.Dialogs
  • 使用相關的Windows API調用或
  • 使用Windows API Code Pack

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

    注意,這個對話框是不可用的操作系統比Windows可見年長ta,所以一定要先檢查CommonFileDialog.IsPlatformSupported

+2

3.5中的Winforms和4.0中的WPF都已更新爲使用Vista對話框。 – 2012-02-10 12:57:01

+0

@HansPassant:不,我只是試過它:'在一個.NET 4.0 WinForms應用程序中顯示新的FolderBrowserDialog()。ShowDialog();'顯示相同的舊的,醜陋的FolderBrowserDialog。 – Heinzi 2012-02-10 13:01:42

+0

檢查FileDialog.AutoUpgradeEnabled屬性。 – 2012-02-10 13:09:00

相關問題