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:
There may be some "higher level" features to get it all, but at least it works.
ken2k Feb 15 2018-12-15T00: 00Z
source share