Answer the question
In order to leave comments, you need to log in
The file is busy with its own process ....!?
Here's the thing... If I open a file in my program and try to save the same file, I get an error that the file is being occupied by another process... And then mind.exe has stopping working... And now how do I release " from under itself" file? I'm missing something, help!
where is the error code:
RichTextBox rtb = tabControl1.SelectedTab.Controls[0] as RichTextBox;
//StreamWriter sr = new StreamWriter(openFileDialog1.FileName, false, Encoding.UTF8);
if (tabControl1.SelectedTab.Text != "new " + tabControl1.SelectedIndex)
{
saveFileDialog1.FileName = GetFileName(tabControl1.SelectedTab.Text);
}
if (tabControl1.TabPages.Count == 0)
{
newTab();
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (!File.Exists(saveFileDialog1.FileName))
{
rtb.SaveFile(GetFileName(saveFileDialog1.FileName), RichTextBoxStreamType.PlainText);
tabControl1.SelectedTab.Text = GetFileName(saveFileDialog1.FileName);
}
else
{
File.Delete(saveFileDialog1.FileName);
rtb.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
tabControl1.SelectedTab.Text = GetFileName(saveFileDialog1.FileName);
}
}
Answer the question
In order to leave comments, you need to log in
You cannot save the file you are reading. You can open a file for reading and you can open it for writing. A file stream cannot be opened simultaneously in two modes, both for reading and for writing. If you opened a file for reading, then before overwriting it, the stream must be closed.
In your case, you may not have used using or dispose, something that will close the stream, or explicitly close the stream.
In general, the order is as follows: opened the file for reading (received text or bytes), closed the stream, changed the contents, opened the file for writing, overwritten, closed the stream.
If you use the File class to work with files, then it closes automatically. If the stream is opened, for example, through a StreamWriter, then you need to monitor the closure yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question