问题
i'm trying to save my txt file, but when i do i get the error in the title? if i use .CreateNew i dont get the error, but i want to save to the existing file i have?
private void OpenFile_MouseDown(object sender, MouseEventArgs e)
{
Stream myStream;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog.OpenFile()) != null)
{
string strfilename = openFileDialog.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox.Text = filetext;
}
}
}
private void savefile_MouseDown(object sender, MouseEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
using (Stream s = File.Open(saveFileDialog.FileName, FileMode.Append))
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(richTextBox.Text);
}
}
回答1:
You should dispose myStream
variable. That's why you are getting that error.
来源:https://stackoverflow.com/questions/62893258/system-io-ioexception-the-process-cannot-access-the-file-because-it-is-being-u