Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an IO exception get thrown if the disk is full?

Tags:

c#

.net

What exception is thrown in the .NET Framework when trying to write a file but the disk is full?

Does Windows buffer file writes?

like image 851
Jack Kada Avatar asked Jan 21 '26 01:01

Jack Kada


2 Answers

You will get an IO exception:

System.IO.IOException: There is not enough space on the disk.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

The System.IO library handles the actual tasks of reading and writing to disk and wraps them in managed code, so you shouldn't get any unmanaged exceptions using them.

It's also worth trying this (with one of those shoddy small USB sticks they give out everywhere) to see what happens with your code - that's usually the best way of finding out this sort of thing.

like image 61
Keith Avatar answered Jan 23 '26 13:01

Keith


You will get an 'IOException'. But the problem is that 'IOException' is quite broad spectrum, not only disk full. I recommend to also check the 'HResult' inside to precisely filter out this case.

                catch (IOException ioEx)
                {
                    // Is it 'DISK_FULL'?
                    uint unsignedResult = (uint)ioEx.HResult;
                    if (unsignedResult.IsOneOf(0x80000027, 0x80000070, 0x80070027, 0x80070070))
                    {
                        // Remove the partial file
                        try
                        {
                            File.Delete(pathName);
                        }
                        catch { }
                        // do your special stuff here
                    }
                    else
                        throw;
                }

Note that the Win32 error codes from region 8000 are mirrored in the region 8007, thats why there is a near duplicate in the checks.

like image 37
AndresRohrAtlasInformatik Avatar answered Jan 23 '26 14:01

AndresRohrAtlasInformatik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!