I have this desktop application I am working on that should keep track of small comments of certain things. The idea is to have a savefiledialog for saving files and and openfiledialog for opening files. But in the openToolStripMenuItem_Click method I have trouble reading from a file and setting the textbox1.Text to the file being read to and opening the text in the file to be edited for small comments. Here is the code below
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fo = new OpenFileDialog();
fo.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
fo.ShowDialog();
if (File.Exists(fo.FileName))
{
using (StreamReader reader = new StreamReader(fo.FileName,Encoding.UTF8)) {
textBox1.Text = reader.ReadToEnd();
}
}
}
this works fine
using (StreamReader reader = new StreamReader(fo.FileName,Encoding.UTF8)) {
textBox1.Text = reader.ReadToEnd();
}
and so does this
textbox1.Text = File.ReadAllText(fo.FileName);
EDIT: There was nothing in the file I was opening.