如何在Visual Studio 2010中使用C#中的OpenFileDialog

时间:2022-12-08 07:20:00

I have written a custom dialog (form) that I can use in a C# program that behaves much like a "File - Open" menu command and brings up a window where a user can select a file or directory.

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

The question I have is this. It has "My Computer" as its root. How can I have it so that it searches on a Network? If the file or directory is located on a network.

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

Or better yet, in Visual Studio 2010, is there some sort of canned FileOpenDialog that I can use right away?

或者更好的是,在Visual Studio 2010中,是否有一些我可以立即使用的固定FileOpenDialog?

I tried calling the OpenFileDialog as described in the example code at http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx but the compiler does not seem to like DialogResult.OK as used in this line of code:

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

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

The compiler says:

编译器说:

Error 1 'System.Nullable' does not contain a definition for 'OK' and no extension method 'OK' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)

错误1'System.Nullable'不包含'OK'的定义,并且没有扩展方法'OK'可以找到接受类型'System.Nullable'的第一个参数(你是否缺少using指令或汇编引用?)

I tried using the namespace Microsoft.Win32 instead of System.Windows.Forms and neither worked. They both produced this error.

我尝试使用命名空间Microsoft.Win32而不是System.Windows.Forms,但都没有工作。他们都产生了这个错误。

2 个解决方案

#1


9  

Looks like you are trying to use a WinForms (System.Windows.Forms) dialog.
Here is the MSDN page for WPF dialog boxes from the Microsoft.Win32 namespace.

看起来您正在尝试使用WinForms(System.Windows.Forms)对话框。以下是Microsoft.Win32命名空间中WPF对话框的MSDN页面。

An excerpt:

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


2  

EDIT: missed the WPF tag. My bad. What Henk Holterman said.

编辑:错过了WPF标签。我的错。亨克霍尔特曼说的话。

Have you added the namespace that the example tells you to: System.IO?

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

I might be wrong, but it sounds like you have created a variable called DialogResult which is of type System.Nullable

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

#1


9  

Looks like you are trying to use a WinForms (System.Windows.Forms) dialog.
Here is the MSDN page for WPF dialog boxes from the Microsoft.Win32 namespace.

看起来您正在尝试使用WinForms(System.Windows.Forms)对话框。以下是Microsoft.Win32命名空间中WPF对话框的MSDN页面。

An excerpt:

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


2  

EDIT: missed the WPF tag. My bad. What Henk Holterman said.

编辑:错过了WPF标签。我的错。亨克霍尔特曼说的话。

Have you added the namespace that the example tells you to: System.IO?

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

I might be wrong, but it sounds like you have created a variable called DialogResult which is of type System.Nullable

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