【问题标题】:How does one use OpenFileDialog in C# in visual Studio 2010如何在 Visual Studio 2010 的 C# 中使用 OpenFileDialog
【发布时间】:2011-02-16 22:17:17
【问题描述】:

我编写了一个自定义对话框(表单),我可以在 C# 程序中使用它,它的行为很像“文件 - 打开”菜单命令,并打开一个窗口,用户可以在其中选择文件或目录。

我的问题是这样的。它以“我的电脑”为根。我怎样才能让它在网络上搜索?如果文件或目录位于网络上。

或者更好的是,在 Visual Studio 2010 中,是否有某种我可以立即使用的预制 FileOpenDialog?

我尝试按照示例代码中的说明调用 OpenFileDialog http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx 但编译器似乎不喜欢这行代码中使用的DialogResult.OK

if(openFileDialog1.ShowDialog() == DialogResult.OK)

编译器说:

错误 1“System.Nullable”不包含“OK”的定义,并且找不到接受“System.Nullable”类型的第一个参数的扩展方法“OK”(您是否缺少 using 指令或程序集参考?)

我尝试使用命名空间 Microsoft.Win32 而不是 System.Windows.Forms,但均未奏效。他们都产生了这个错误。

【问题讨论】:

    标签: c# wpf visual-studio visual-studio-2010 openfiledialog


    【解决方案1】:

    您似乎正在尝试使用 WinForms (System.Windows.Forms) 对话框。
    这是来自Microsoft.Win32 命名空间的MSDN page for WPF dialog boxes

    摘录:

    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    dlg.FileName = "Document"; // Default file name
    dlg.DefaultExt = ".txt"; // Default file extension
    dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
    
    // Show open file dialog box
    bool? result = dlg.ShowDialog();
    

    【讨论】:

      【解决方案2】:

      编辑:错过了 WPF 标签。我的错。 Henk Holterman 所说的。

      您是否添加了示例告诉您的命名空间:System.IO

      我可能错了,但听起来您创建了一个名为 DialogResult 的变量,其类型为 System.Nullable

      【讨论】:

        猜你喜欢
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 1970-01-01
        • 2011-08-11
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        相关资源
        最近更新 更多