打开目录对话框 [英] Open directory dialog

查看:425
本文介绍了打开目录对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户选择一个目录,我将生成的文件将被保存。我知道在WPF中,我应该使用Win32中的 OpenFileDialog ,但不幸的是,该对话框需要选择文件 - 如果我只是单击确定而不选择一个。我可以通过让用户选择一个文件,然后剥离路径来找出它所属的目录,但是最好是不直观的,从而攻击功能。有没有人看过这样做?

I want the user to select a directory where a file that I will then generate will be saved. I know that in WPF I should use the OpenFileDialog from Win32, but unfortunately the dialog requires file(s) to be selected - it stays open if I simply click OK without choosing one. I could "hack up" the functionality by letting the user pick a file and then strip the path to figure out which directory it belongs to but that's unintuitive at best. Has anyone seen this done before?

推荐答案

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

You can use the built-in FolderBrowserDialog class for this. Don't mind that it's in the System.Windows.Forms namespace.

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

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

If you want the window to be modal over some WPF window, see the question How to use a FolderBrowserDialog from a WPF application.

编辑:如果您想要的东西比普通,丑陋的Windows Forms FolderBrowserDialog有点多,可以使用一些替代方法来使用Vista对话框:

If you want something a bit more fancy than the plain, ugly Windows Forms FolderBrowserDialog, there are some alternatives that allow you to use the Vista dialog instead:

  • Third-party libraries, such as Ookii dialogs (.NET 3.5)
  • The Windows API Code Pack-Shell:

using Microsoft.WindowsAPICodePack.Dialogs;

...

var dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
CommonFileDialogResult result = dialog.ShowDialog();

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

Note that this dialog is not available on operating systems older than Windows Vista, so be sure to check CommonFileDialog.IsPlatformSupported first.

这篇关于打开目录对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆