If you create a FileStream, the file will be locked, so StreamWriter will not be able to write to it. I suggest you also learn about the
IDisposable-interface and the
using-pattern. Anyway, there's actually a simpler solution for this problem: the static
File-class has some useful methods for dealing with files, including a method
WriteAllText, which writes text to a file.
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
using(SaveFileDialog save = new SaveFileDialog())
{
save.Filter = "Expression Files (*.exp)|*.exp";
if (save.ShowDialog(this) == DialogResult.OK)
try
{
File.WriteAllText(save.FileName, textBox1.Text);
}
catch(IOException ex)
{
MessageBox.Show(this
, "Could not save the expression: " + ex.Message
, "Error while writing file"
, MessageBoxButtons.OK
, MessageBoxIcon.Error);
}
}
}