My .NET Windows application has multiple instances of being able to open and view text files. I have no problem getting this to work when I create individual openFileDialogs through my code. In trying to clean up my code, I created a method, but I am fairly new and am getting a red squiggly. Can someone please look at this and tell me what I am doing wrong? Thanks.
I am trying to call the method as shown here:
textBox_ViewFile.Text = LoadFromFile();
Then my method is as follows:
static string[] LoadFromFile()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = false;
openFileDialog1.ShowHelp = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if (openFileDialog1.OpenFile() != null)
{
return (File.ReadAllLines(openFileDialog1.FileName));
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
In this case, I am getting a syntax error in VS2010 under “LoadFromFile()” from
static string[] LoadFromFile()"
You’re missing a
returnstatement if either of yourifstatements arefalse