[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1551
  • Last Modified:

what exception should I catch for disk full?

Hi,
I am writing a windows application using c#. this needs application needs to keep writing csv file. my question is what exception I should catch if system is runing out of disk space.

thanks
0
viola123
Asked:
viola123
  • 2
1 Solution
 
CartoonHeadCommented:
You can test available disk space using:
DriveInfo di= new DriveInfo("C:");
long freeSpace = di.TotalFreeSpace;
and the exeption would be IOException.

Probably better to test you have space available before attemting the write though.
0
 
DhaestCommented:
You can add the general error, and after you got the right error you can perhaps specify it a little bit better (I'm not sure if there is a specific diskfull-exception)
Example
try
    {
      // We're simply copying a file here.
      System.IO.File.Copy(strFile, strFile + ".new");
    }
    catch (Exception ex)
    {
      Console.WriteLine("Failed to copy the file: " +  ex.Message);
    }
0
 
philipjonathanCommented:
It should be IOException.
You can test it out easily by writing to a floppy drive or USB thumbdrive (which is near full) ;)
0
 
philipjonathanCommented:
It should be IOException.
You can test it out easily by writing to a floppy drive or USB thumbdrive (which is near full) ;)

If you are unsure, follow what Dhaest suggested by catching a generic exception and simulate the disk full scenario. Then you can find out the Exception type by putting a breakpoint on the catch line and open your "Watch" panel in VS.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now