You need to check the result of the dialog:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
using (var openFile = new StreamReader(openFileDialog1.FileName)) {
getRichTextBox().Text = OpenFile.ReadToEnd();
}
}
}
I've also added a using
statement to ensure that your file is closed when you're done reading it.
You can simplify the code even further by simply using File.ReadAllText
instead of messing around with StreamReader
.
getRichTextBox().Text = File.ReadAllText(openFileDialog1.FileName);
(Thanks @keyboardP)