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;
OpenFileD开发者_JAVA百科ialog 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);
}
}
}
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.
It sounds like you have a local property called DialogResult
. Try using System.Windows.Forms.DialogResult.OK
instead.
It looks like its trying to use ShowDialog for System.Windows.Controls
.
Try making the call explicit to System.Windows.Forms
Like:
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
精彩评论