"Karl" <ka*******@insidertech.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,
I have some code that will save the contents of a Rich Text Box in
either a Text or Rich Text Format file. The code is using the
SaveFileDialog and is working correctly.
I have been testing the code and added in some exception handling to
cater for any problems. During testing I have found that if I attempt
to save to a floppy disc that is full, a System.IO.IOException is raied
with the message "There is not enough space on the disk". I am catching
this exception to display a message to the user. I then Dispose of the
SaveFileDialog. I then perform Garbage Collection via GC.Collect and at
this point the exception is raised again but is not caught. Even if a
put a try around the GC.Collect and have a subsequent Catch the
exception is now handled.
Can someone explain what I'm doing wrong? I'm guessing that the
SaveFileDialog is retrying the save and I need to somehow flush it or
abort?
The code I'm using :
try
{
richTextBox.SaveFile (saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}
catch (System.IO.IOException ex)
{
MessageBox.Show ("Error: " + ex.Message + "\n",
"Problem Saving the Results", MessageBoxButtons.OK);
}
saveFileDialog1.Dispose (); // Dispose of the save file dialog
try
{
GC.Collect (); // collect the garbage
}
catch (System.IO.IOException ex)
{
MessageBox.Show ("Error: " + ex.Message + "\n",
"Problem Saving the Results v2", MessageBoxButtons.OK);
}
catch ( Exception ex )
{
MessageBox.Show ("Error: " + ex.Message + "\n",
"Problem Saving the Results v3", MessageBoxButtons.OK);
}
The problem is that whatever you try (Dispose, GC etc...) , the function
tries to flush the buffer and this is just the problem, it can't because the
is no space on disk.
What you could do (if possible) before you start writing to a disk is to
have a look whether there is anough free space to write your data. If this
isn't possible, your only option is to close the file using Win32's API
CloseHandle, only problem is how to get at the underlying file handle when
using RichTextBox.SaveFile.
Willy.