打开目录对话框

我希望用户select一个目录,然后我将生成一个文件将被保存。 我知道,在WPF中,我应该使用Win32的OpenFileDialog ,但不幸的是,对话框需要select文件 – 如果我只是单击“确定”而没有select文件,它将保持打开状态。 我可以通过让用户select一个文件来“破解”这个function,然后去掉这个path来找出它属于哪个目录,但是这样做不是直觉。 有没有人见过这样做?

您可以使用内置的FolderBrowserDialog类。 不要介意它在System.Windows.Forms命名空间中。

 using (var dialog = new System.Windows.Forms.FolderBrowserDialog()) { System.Windows.Forms.DialogResult result = dialog.ShowDialog(); } 

如果您希望窗口在某些WPF窗口上模态化,请参阅问题如何使用WPF应用程序中的FolderBrowserDialog 。


编辑:如果你想比普通,丑陋的Windows窗体FolderBrowserDialog多一点花式,有一些替代scheme,允许您使用Vista对话框:

  • 第三方库,如Ookii对话框 (.NET 3.5)
  • Windows API代码包 – shell :

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

    请注意,此对话框在Windows Vista之前的操作系统上不可用,因此请务必首先检查CommonFileDialog.IsPlatformSupported

我创build了一个像这样使用的UserControl:

  <UtilitiesWPF:FolderEntry Text="{Binding Path=LogFolder}" Description="Folder for log files"/> 

xaml源代码如下所示:

 <UserControl x:Class="Utilities.WPF.FolderEntry" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <DockPanel> <Button Margin="0" Padding="0" DockPanel.Dock="Right" Width="Auto" Click="BrowseFolder">...</Button> <TextBox Height="Auto" HorizontalAlignment="Stretch" DockPanel.Dock="Right" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" /> </DockPanel> </UserControl> 

和代码隐藏

 public partial class FolderEntry { public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(FolderEntry), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); public static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(FolderEntry), new PropertyMetadata(null)); public string Text { get { return GetValue(TextProperty) as string; } set { SetValue(TextProperty, value); }} public string Description { get { return GetValue(DescriptionProperty) as string; } set { SetValue(DescriptionProperty, value); } } public FolderEntry() { InitializeComponent(); } private void BrowseFolder(object sender, RoutedEventArgs e) { using (FolderBrowserDialog dlg = new FolderBrowserDialog()) { dlg.Description = Description; dlg.SelectedPath = Text; dlg.ShowNewFolderButton = true; DialogResult result = dlg.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { Text = dlg.SelectedPath; BindingExpression be = GetBindingExpression(TextProperty); if (be != null) be.UpdateSource(); } } } } 

我使用Ookii对话框一段时间,它对WPF很好。

这是直接页面:

http://www.ookii.org/Blog/new_download_ookiidialogs

对于目录对话框来获取目录path,首先添加引用System.Windows.Forms,然后parsing,然后把这个代码放在一个button中单击。

  var dialog = new FolderBrowserDialog(); dialog.ShowDialog(); folderpathTB.Text = dialog.SelectedPath; 

(folderpathTB是文本框的名称,我wana把文件夹path,或者你也可以把它分配给一个stringvariables,也就是说)

  string folder = dialog.SelectedPath; 

如果你得到文件名/path,只需button点击

  FileDialog fileDialog = new OpenFileDialog(); fileDialog.ShowDialog(); folderpathTB.Text = fileDialog.FileName; 

(folderpathTB是TextBox的名称,我把wana放在文件path中,或者你也可以把它分配给一个stringvariables)

注意:对于文件夹对话框,必须将System.Windows.Forms.dll添加到项目中,否则将无法工作。

我发现下面的链接下面的代码…它工作select文件夹对话框WPF

 using Microsoft.WindowsAPICodePack.Dialogs; var dlg = new CommonOpenFileDialog(); dlg.Title = "My Title"; dlg.IsFolderPicker = true; dlg.InitialDirectory = currentDirectory; dlg.AddToMostRecentlyUsedList = false; dlg.AllowNonFileSystemItems = false; dlg.DefaultDirectory = currentDirectory; dlg.EnsureFileExists = true; dlg.EnsurePathExists = true; dlg.EnsureReadOnly = false; dlg.EnsureValidNames = true; dlg.Multiselect = false; dlg.ShowPlacesList = true; if (dlg.ShowDialog() == CommonFileDialogResult.Ok) { var folder = dlg.FileName; // Do something with selected folder string } 

实现你想要的最好的方法是创build你自己的基于wpf的控件,或者使用其他人创build的控件
为什么? 因为在wpf应用程序中使用winforms对话框时会有明显的性能影响(出于某种原因)
我推荐这个项目
https://opendialog.codeplex.com/
或者Nuget:

 PM> Install-Package OpenDialog 

它非常适合MVVM,它不包含winforms对话框

Ookii文件夹对话框可以在Nugetfind。

PM> Install-Package Ookii.Dialogs

而且,示例代码如下所示。

 var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog(); if (dialog.ShowDialog(this).GetValueOrDefault()) { textBoxFolderPath.Text = dialog.SelectedPath; } 

Ookii VistaFolderBrowserDialog是你想要的。

如果你只需要Ooki Dialogs的文件夹浏览器,而没有其他的东西,那么下载Source ,select你需要的文件夹浏览器的文件(提示:7个文件),并且在.NET 4.5.2中编译好。 我不得不添加一个对System.Drawing的引用。 将原始项目中的参考文献与您的项目进行比较。

你如何找出哪些文件? 在不同的Visual Studio实例中打开您的应用程序和Ookii。 添加VistaFolderBrowserDialog.cs到你的应用程序,并不断添加文件,直到构build错误消失。 您可以在Ookii项目中find依赖关系 – 控制 – 单击您要追溯到其源(双关意图)。

如果你懒得这样做的话,这里是你需要的文件…

 NativeMethods.cs SafeHandles.cs VistaFolderBrowserDialog.cs \ Interop COMGuids.cs ErrorHelper.cs ShellComInterfaces.cs ShellWrapperDefinitions.cs 

编辑VistaFolderBrowserDialog.cs第197行,除非你想包含他们的Resources.Resx

抛出新的InvalidOperationException(Properties.Resources.FolderBrowserDialogNoRootFolder);

 throw new InvalidOperationException("Unable to retrieve the root folder."); 

按照他们的license.txt将他们的版权声明添加到您的应用程序

\Ookii.Dialogs.Wpf.Sample\MainWindow.xaml.cs行160-169中的代码是一个您可以使用的示例,但您需要从MessageBox.Show(this, for WPF)中删除this,

适用于我的电脑[TM]

我build议,添加在块金包:

  Install-Package OpenDialog 

那么使用它的方式是:

  Gat.Controls.OpenDialogView openDialog = new Gat.Controls.OpenDialogView(); Gat.Controls.OpenDialogViewModel vm = (Gat.Controls.OpenDialogViewModel)openDialog.DataContext; vm.IsDirectoryChooser = true; vm.Show(); WPFLabel.Text = vm.SelectedFilePath.ToString(); 

这里是文档: http : //opendialog.codeplex.com/documentation

适用于文件,带有filter,文件夹等的文件

我知道这是一个老问题,但一个简单的方法是使用WPF提供的FileDialog选项,并使用System.IO.Path.GetDirectory(filename)。

这些答案都没有为我工作(通常有一个失踪的参考或沿着这些线路)

但是这很简单:

在WPF应用程序中使用FolderBrowserDialog

添加对System.Windows.Forms的引用并使用以下代码:

  var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult result = dialog.ShowDialog(); 

无需追踪丢失的包裹。 或者添加巨大的课程

这给了我一个现代文件夹select器,也允许你创build一个新的文件夹

我还没有看到部署到其他机器的影响