2010-08-17 109 views
6

我想从here获取一个基本的文件对话框示例,并且在'OK'上出现错误,我不知道为什么。'System.Nullable <bool>'不包含'OK'的定义

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

private void button1_Click(object sender, System.EventArgs e) 
{ 
    Stream myStream = null; 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

    openFileDialog1.InitialDirectory = "c:\\" ; 
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; 
    openFileDialog1.FilterIndex = 2 ; 
    openFileDialog1.RestoreDirectory = true ; 

    if(openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     try 
     { 
      if ((myStream = openFileDialog1.OpenFile()) != null) 
      { 
       using (myStream) 
       { 
        // Insert code to read the stream here. 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
     } 
    } 
} 
+0

你打开文件对话框什么组件可从? – 2010-08-17 15:06:15

+0

这个工作,你可以更具体一点?错误可能不在这里... – jeroenh 2010-08-17 15:06:22

回答

16

.NET framework中有两个版本的OpenFileDialogWinForms oneWPF one。看起来你使用的是WPF,事实上,它返回的Nullable<bool>值。 WinForm版本返回一个DialogResult值,这似乎是你所期望的。

+0

您链接的版本是WIN32版本。我不认为有一个特定于WPF的。 – pug 2013-02-15 18:37:42

+1

@pug请注意,所讨论的类是在PresentationFramework程序集中定义的,该程序集是WPF的核心程序集之一。 – 2013-02-15 19:01:15

8

这听起来像你有一个名为DialogResult本地属性。请尝试使用System.Windows.Forms.DialogResult.OK

+0

这对我有效。谢谢!!!!! – jjones150 2016-07-21 21:18:54

1

它看起来像它试图使用ShowDialogSystem.Windows.Controls。 尝试进行调用明确到System.Windows.Forms

像:

System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 
相关问题