I have a console application which do "Screen Capture", and save the images in "c:/" directory with infinite number of pictures, I want the application to keep capturing and saving pictures until the hard disk become full WITHOUT crashing the application. So how to achieve that please?
Asked
Active
Viewed 303 times
-1
-
This sounds like a bad idea, but that's none of my business. Regardless, you can get the remaining space after each save and compare it to total space for that partition. Won't be very accurate, but it's close. Also, for future questions, please include some code and show your attempt in solving the problem. Otherwise, you question will most likely be deleted. – Arian Motamedi Nov 24 '14 at 23:41
-
Use DriveInfo.AvailableFreeSpace periodically. Quit when it drops below a gig or two. – Hans Passant Nov 24 '14 at 23:55
1 Answers
0
How to keep the application from crashing? Simple: catch the exception that occurs when you try to save the file to a full drive. For example:
var image = GetScreenImage(); // or however you're capturing the screen
try
{
image.Save(filename);
}
catch (Exception ex)
{
// handle the exception here.
}
You don't say what kind of image you're getting, so I can't give you an exact scenario. But the concept is the same: if the attempt to save an image throws an exception, catch the exception, clean up any streams or whatever that you might have open, and then just continue. Display an error message if you want. Log an error to a file, although you'll probably want that log file to be on a different drive.
That will prevent your application from crashing. No telling what it will do to any other running applications that need to write to the disk.

Jim Mischel
- 131,090
- 20
- 188
- 351
-
1Please don't ever do a `catch (Exception ex)` as this is almost as bad as using `goto`. In this case, from memory, `System.IO.IOException` is the exception to catch. – Enigmativity Nov 24 '14 at 23:55