IOException (disk full) problem
I using this code to save a file:
private void saveToolStripButton_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
try
{
// Opens a file and serializes the object into it in XML format.
Stream stream = File.Open(saveFileDialog.FileName, FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, lev);
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show(
"Could not write file to disk: "
+ ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
it work and if i try to save the file to a full disk it show my MessageBox, but when i close my application it generate one more disk full IOException.
The same thing happen if I call stream.Close(); inside catch block.
Thank you for help.
Bye
Toni
where exactly in your code does it generate the extra IOExceptions? you should be also using a finally {...} block to close the stream even after the exception being thrown to make sure its closed properly or perhaps a using{...} block:
private void saveToolStripButton_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
using (Stream theStream = File.Open(saveFileDialog.FileName, FileMode.Create))
{
try
{
// Opens a file and serializes the object into it in XML format.
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, lev);
}
catch (Exception ex)
{
MessageBox.Show("Could not write file to disk: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
would be nice to tell us where the other exceptions are being thrown when you close your program
Thank you for help.
When I close the application the extra IOExceptions is not generated inside my code: Visual Studio don't go to specific code line.
I suppose that the problem is becouse system try to close file when i close application and
before close it do automatic Flush().If I use your
using (Stream theStream = File.Open(saveFileDialog.FileName, FileMode.Create))
the exception happen at the } that close this using block.
I found this problem writing to about full floppy disk.
sure, kind of guessed that even using the using block would have the same effect but thought it would clean up itself there and then to prevent the other IOExceptions from occuring perhaps. You are right that it will try to flush everything to disk when you close the application/leave the block.
it is an interesting one and will see what else I can find
Here the solution at this problem:
private void saveToolStripButton_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
try
{
using (Stream stream = File.Open(saveFileDialog.FileName, FileMode.Create))
{
try
{
// Opens a file and serializes the object into it in XML format.
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, lev);
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show(
"Could not write file to disk: "
+ ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
catch (Exception)
{
//MessageBox.Show("Same error");
}
}
}
Toni