【问题标题】:Using FolderBrowserDialog in WPF application [duplicate]在 WPF 应用程序中使用 FolderBrowserDialog [重复]
【发布时间】:2011-05-31 15:29:00
【问题描述】:

我有一个 WPF 应用程序,我需要让用户访问其中的目录。关于如何将 Windows 窗体集成到 WPF 中,我已经搜索到了世界尽头,并找到了有关如何将表单控件集成到我的然而,xaml 集成了一个 FolderBrowserDialog...

我是资深程序员,但对 .net 非常陌生(实际上是第 2 天),我相信我找不到有关实施此功能的好信息,因为我无法确定 FolderBrowserDialog 的名称/类型。

请帮忙。谢谢, -大卫

哦,我正在使用 c# 和 Visual Studio 2008

【问题讨论】:

    标签: c# .net wpf folderbrowserdialog windowsformsintegration


    【解决方案1】:

    您需要添加对 System.Windows.Forms.dll 的引用,然后使用System.Windows.Forms.FolderBrowserDialog class

    添加using WinForms = System.Windows.Forms; 会有所帮助。

    【讨论】:

    • 不幸的是,我已经尝试过,我收到此错误:错误 1 ​​名称空间“System.Windows”中不存在类型或名称空间名称“Forms”(您是否缺少程序集引用?)
    • @David:正如我所说,您需要添加对 System.Windows.Forms.dll 的引用
    • 不幸的是它给了我同样的错误。
    • @david:这意味着您没有添加参考。右键单击该项目,然后单击添加引用。
    • using WinForms = System.Windows.Forms; 确实帮助我避免了模棱两可的引用。谢谢!
    【解决方案2】:

    如果我没记错的话,您正在寻找FolderBrowserDialog(因此得名):

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

    另见此 SO 线程:Open directory dialog

    【讨论】:

    • 使用 var 作为类型会在 C# 中为我产生错误,并用 FolderBrowserDialog 替换它,就像我在其他地方看到的那样会产生缺少指令错误,即使使用 System.Windows.Forms.FolderBrowserDialog 指令.还有什么我做错了吗?我会立即查看该链接。谢谢
    • 这应该可以工作 - 正如 SLaks 指出的,您需要在项目中添加对 System.Windows.Forms.dll 的引用
    • 您还应该检查结果。例如: if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = dialog.SelectedPath; }
    • 引用中的任何内容都没有实际工作(引用中缺少类),但这对我来说非常有效。
    猜你喜欢
    • 2010-09-23
    • 1970-01-01
    • 2012-08-30
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多