c# – ‘System.Nullable’不包含’OK’的定义

前端之家收集整理的这篇文章主要介绍了c# – ‘System.Nullable’不包含’OK’的定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从 here开始的基本文件对话框示例,我在’确定’上收到错误,我不知道为什么.

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?)

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);
        }
    }
}

解决方法

.NET框架中有两个版本的OpenFileDialog: WinForms oneWPF one.看起来你正在使用WPF,实际上它确实返回了Nullable< bool>来自OpenFile的价值. WinForm版本返回一个DialogResult值,这似乎是您所期望的.

猜你在找的C#相关文章