How to detect out of disk space?

Does anyone have any good suggestions on how to detect when an "out of disk space" condition is encountered

When such a condition is encountered, a System.IO.IOException is raised.  In English, the exception message is currently "There is not enough space on the disk."

The basic problem is that other errors can also cause a System.IO.IOException (like a read/write error), so how can one determine if the exception raised was due to out of disk space or some other reason  

It is recommended that you do not make logic decisions based upon the actual message as the messages may change, and actually will vary based upon the language that computer is configured to use.

I guess one approach could be to check for available disk space and if it's a low number assume it's an out of disk space error.  But since I don't always know how much disk space is being required (like when saving a dataset to disk), such an approach may not always be accurate.

Does anyone have any other suggestions

TIA,

Richard


Answer this question

How to detect out of disk space?

  • stoffi

    If, while writing a file, my application received a System.IO.IOException which was triggered by running out of disk space, I could display a message to the user explaining that the drive specified ran out of disk space, and to either free up some disk space or give them the option of saving the file to another drive.

    On the other hand, if the exception was raised to do some I/O error (say for example, a bad sector or some other physical issue), then the message would need to be different.  Something more along the line of a disk error was detected and that you should report it to your computer support people.

    Frankly, I am completely baffled why Microsoft would choose to utilize the same exception for both type of conditions.

    Personally, I would prefer to be able to handle the issue strictly utilizing the information returned by the exception.  I just don't see how to differitiate between the two type of conditions (out of disk space, some sort of hardware issue) without resulting to either examining the exception message or checking the drive for available space.  Neither of which I consider great options.  Any suggestions would be welcome.

    Richard

  • JHT

    Hello Richard,

    Can you explain a bit more about your scenario For example, what would you like to do if the disk space is low Retry Display a message to the user

    I would definitely not recommend writing any code to try to guess about the disk space. Disk space is a very time-sensitive aspect of your operating environment.

    Factors that could affect disk space:
    - Fragmentation
    - Other applications, such as video/audio editing
    - Back up software
    - disk mirroring/merging
    - hardware failure
    - Network availability
    - Virtual operating environments (e.g. VirtualPC)

    Note that network shares are notoriously unpredictable, especially due to the behavior of networks (wireless!).

    Trying to respond to the "disk space" issue dependent on a specific type of 'disk space' environment may be similar to trying to respond to a memory allocation failure by trying to determine issues of heap fragmentation, corruption, or allocation pattern.

    Hope that helps,
    Stephen [Microsoft Common Language Runtime: Security - Developer]
    http://blogs.msdn.com/stfisher

  • How to detect out of disk space?