How to check if an IOException is a type of Not-Enough-Disk-Space-Exception?

How to check if an IOException type of "Out of disk space" exception?

I am currently checking to see if the message matches something like "Not enough disk space", but I know that this will not work if the OS language is not English.

+59
c # exception-handling diskspace ioexception
Feb 15 '12 at 12:18
source share
5 answers

You need to check HResult and test ERROR_DISK_FULL (0x70) and ERROR_HANDLE_DISK_FULL (0x27) , which can be converted to HResults on OR 'ing using 0x80070000 .

For .Net Framework 4.5 and higher, you can use the Exception.HResult property:

 static bool IsDiskFull(Exception ex) { const int HR_ERROR_HANDLE_DISK_FULL = unchecked((int)0x80070027); const int HR_ERROR_DISK_FULL = unchecked((int)0x80070070); return ex.HResult == HR_ERROR_HANDLE_DISK_FULL || ex.HResult == HR_ERROR_DISK_FULL; } 

For older versions, you can use Marshal.GetHRForException to return HResult, but this one has significant side effects and is not recommended :

 static bool IsDiskFull(Exception ex) { const int ERROR_HANDLE_DISK_FULL = 0x27; const int ERROR_DISK_FULL = 0x70; int win32ErrorCode = Marshal.GetHRForException(ex) & 0xFFFF; return win32ErrorCode == ERROR_HANDLE_DISK_FULL || win32ErrorCode == ERROR_DISK_FULL; } 

From the MSDN documentation:

Note that the GetHRForException method sets IErrorInfo to the current thread. This may lead to unexpected results for methods such as ThrowExceptionForHR , which by default use the IErrorInfo current thread, if one is installed.

See also How to define HResult for System.IO.IOException?

+70
Feb 15 2018-12-15T00:
source share

In .NET 4.5, the getter HResult property is now open, so you no longer need to use the Marshal.GetHRForException (along with its side effects).

http://msdn.microsoft.com/en-us/library/system.exception.hresult(v=vs.110).aspx states: "Starting with the .NET Framework 4.5, the HResult property definition tool is protected, while its recipient is publicly available. versions of the .NET Framework, both getter and setter are protected "

So you can use Justin's answer, but replace Marshal.GetHRForException(ex) with ex.HResult .

+20
Mar 31 '14 at 13:46
source share

Well, it's a bit hacky, but here we go.

The first thing to do is get HResult from the exception. Since this is a protected member, we need to flip a bit to get the value. Here the extension method will do the trick:

 public static class ExceptionExtensions { public static int HResultPublic(this Exception exception) { var hResult = exception.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(z => z.Name.Equals("HResult")).First(); return (int)hResult.GetValue(exception, null); } } 

Now, in the capture area, you can get HResult :

 catch (Exception ex) { int hResult = ex.HResultPublic(); } 

From here you will have to interpret HResult. You will need this link .

We need to get an ErrorCode that is stored in the first 16 bits of the value, so here is some bit operation:

 int errorCode = (int)(hResult & 0x0000FFFF); 

Now refer to the list of system error codes , and here we are:

 ERROR_DISK_FULL 112 (0x70) 

So test it using:

 switch (errorCode) { case 112: // Disk full } 

There may be some "higher level" features to get it all, but at least it works.

+13
Feb 15 2018-12-15T00:
source share

The simplest built-in solution (min. .NET 4.5 and C # 6):

 try { //... } catch (IOException ex) when ((ex.HResult & 0xFFFF) == 0x27 || (ex.HResult & 0xFFFF) == 0x70) { //... } 
+9
Apr 15 '16 at 10:24
source share

System.IOException has a number of derived exception types, however, none of these derived types sounds like your exception. You can look at the HResult or Data property for an exception, perhaps this will contain a more detailed error code. According to MSDN, both of these properties are part of this type of exception. Just make sure you are trying to catch a specific type of exception, not just the base type of exception.

+1
Feb 15 2018-12-15T00:
source share



All Articles