I’m a complete beginner to this language and am currently trying to create a simple text editing application, but am getting the above error when trying to open a file.
The code’s below, any help would be much appreciated.
private void mnuOpen_Click(object sender, EventArgs e)
{
string chosenFile = "";
openFD.Title = "Select a text file";
openFD.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFD.FileName = "Choose a file..";
openFD.Filter = "Text Files|*.txt|All Files|*.*";
if (openFD.ShowDialog() != DialogResult.Cancel)
{
contentBox.LoadFile(chosenFile, RichTextBoxStreamType.PlainText);
}
}
The exception itself occurs here:
contentBox.LoadFile(chosenFile, RichTextBoxStreamType.PlainText);
You need to set
chosenFileto the name of the file selected in the OpenFileDialog control: