Problem handling Disk full exception

K

Karl

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);
}
 
O

Olaf Baeyens

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?
I think you want to do something like this.
No need to call the GC.Collect()

try {
try {
richTextBox.SaveFile (saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
} finally {
saveFileDialog1.Dispose (); // Dispose the save file dialog
exception or not
}
} catch (System.IO.IOException ex) {
MessageBox.Show ("Error: " + ex.Message + "\n",
"Problem Saving the Results v2", MessageBoxButtons.OK);
}
}
 
W

Willy Denoyette [MVP]

Karl said:
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.
 
K

Karl

Thanks Willy, I thought it was something like that occurring. I'm now
going to use the System.Management functions to check if there is room
on the disk before attempting to write.

Thanks

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top