Get error code from Hresult

Hello ,

I want get the ErrorCode of an exception in better way that : “exception.message.Contains”

My first possible approach is to convert/format exception.HResult into error code but i dont found the way to achieve this goal.

Exemple
Actual Hresult : -2146233088
Error code : 1016

I tried this without success : Marshal.GetExceptionForHR(e.HResult).ToString .

Looking for a better way than :

So i suppose your error code will look like this…

I took this screenshot from another post here in the forum…

So after the error code you have a “:”. And also right before the code, we have the word error code…

So what we can do is, we can split the string based on this pattern and access only that element in the array we got after splitting. It will be more efficient than the contains I suppose…

1 Like