What exception is thrown in the .NET Framework when trying to write a file but the disk is full?
Does Windows buffer file writes?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With