如何基于ookii-dialogs-wpf配置多文件夹/文件选择对话框?
Hey there! Let's break down how to handle selecting multiple folders with your current setup.
First, the straight answer: Ookii.Dialogs.WPF's VistaFolderBrowserDialog doesn't support multi-selection natively. That component is built on Windows' Vista-style folder browser, which doesn't include a multi-select feature. But we can work around this using either another tool from the same library or a native WPF dialog trick.
Option 1: Use Ookii's OpenFileDialog to Select Folders
You can repurpose the library's OpenFileDialog to act as a folder picker with multi-select enabled. Here's how to implement it:
private void Btn_select_multiple_folders_Click(object sender, RoutedEventArgs e) { var dialog = new Ookii.Dialogs.Wpf.OpenFileDialog { Filter = "Folders|*.none", // Triggers folder-focused display CheckFileExists = false, CheckPathExists = true, Multiselect = true, Title = "Select multiple folders to save processed clips", FileName = "Select folders" // Ensures the dialog defaults to folder view }; // Enable folder selection mode via the underlying Windows API if (dialog is IFileDialog fileDialog) { fileDialog.GetOptions(out var currentOptions); fileDialog.SetOptions(currentOptions | FOS.FOS_PICKFOLDERS); } if (dialog.ShowDialog(Application.Current.MainWindow).GetValueOrDefault()) { // dialog.FileNames contains all selected folder paths foreach (var folderPath in dialog.FileNames) { // Handle each folder (e.g., update your storage variable) // m_SelectedFolders.Add(folderPath); } } }
You'll need these COM interface definitions to interact with the Windows file dialog API (add them to your project):
using System.Runtime.InteropServices; [ComImport] [Guid("42f85136-db7e-439c-85f1-e4075d135fc8")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] private interface IFileDialog { [PreserveSig] int Show(IntPtr parent); void SetOptions(FOS fos); void GetOptions(out FOS pfos); // We only need these two methods for this use case } [Flags] private enum FOS { FOS_PICKFOLDERS = 0x00000020, // Flag to enable folder selection }
Option 2: Use Native WPF OpenFileDialog (No Extra COM Setup)
If you prefer to avoid the COM interface setup, you can use WPF's built-in OpenFileDialog and tweak it to select folders:
private void Btn_select_multiple_folders_Click(object sender, RoutedEventArgs e) { var dialog = new Microsoft.Win32.OpenFileDialog { Multiselect = true, Title = "Select multiple folders", Filter = "Folders|*.none", CheckFileExists = false, CheckPathExists = true }; // Use reflection to enable folder selection mode typeof(Microsoft.Win32.OpenFileDialog) .GetMethod("SetOption", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .Invoke(dialog, new object[] { 0x20, true }); if (dialog.ShowDialog() == true) { foreach (var folderPath in dialog.FileNames) { // Process each selected folder } } }
Key Notes
- Both approaches work by tricking the file dialog into displaying folders instead of files, while keeping multi-select enabled.
- The returned paths are full folder paths, just like you'd get from
VistaFolderBrowserDialog, so your existing processing logic should work seamlessly.
内容的提问来源于stack exchange,提问作者Sirop4ik

