469,645 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Problem handling Disk full exception


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

Nov 17 '05 #1
5 6955
> 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);
}
}

Nov 17 '05 #2

"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.

Nov 17 '05 #3
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

Nov 17 '05 #4
I recomend to look at Constrained Execution Regions. It's FW 2.0
feature to solve yo problem
http://msdn.microsoft.com/msdnmag/is...y/default.aspx

Nov 17 '05 #5

"Michael Nemtsev" <la*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I recomend to look at Constrained Execution Regions. It's FW 2.0
feature to solve yo problem
http://msdn.microsoft.com/msdnmag/is...y/default.aspx

No, CER have nothing to do with this, please read about what they are and
where they could apply.

Willy.
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gary Robinson | last post: by
27 posts views Thread by garyolsen | last post: by
11 posts views Thread by suzy | last post: by
13 posts views Thread by tolisss | last post: by
4 posts views Thread by Peter Aitken | last post: by
7 posts views Thread by Garth Wells | last post: by
reply views Thread by =?Utf-8?B?UG9sbHkgQW5uYQ==?= | last post: by
1 post views Thread by stephen | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.