C# - How to save many files to the hard disk until it become full without crashing

309 views Asked by At

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?

1

There are 1 answers

1
Jim Mischel On BEST ANSWER

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.