Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.Nullable<bool>' does not contain a definition for 'OK'

Tags:

c#

file

dialog

wpf

I'm trying a basic file dialog example from, here, and I get an error on 'OK', and I don't know why.

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);
        }
    }
}
like image 412
rd42 Avatar asked Aug 17 '10 14:08

rd42


2 Answers

There are two versions of OpenFileDialog in the .NET framework: the WinForms one and the WPF one. It looks like you're using the WPF one, which does, in fact, return a Nullable<bool> value from OpenFile. The WinForm version returns a DialogResult value, which seems to be what you're expecting.

like image 64
Daniel Pratt Avatar answered Sep 17 '22 16:09

Daniel Pratt


It sounds like you have a local property called DialogResult. Try using System.Windows.Forms.DialogResult.OK instead.

like image 28
Marc Gravell Avatar answered Sep 21 '22 16:09

Marc Gravell