4

I am building a C# project and at some point I encounter an ErrorCode in an exception:

try {
   ...
} catch (HttpListenerException e) {
   if (e.ErrorCode == 995) {
   }
}

this works, but what I want to both make my code more readable, as well as to avoid "magic constants", is something like this (using the error code names):

try {
   ...
} catch (HttpListenerException e) {
   if (e.ErrorCode == ERROR_OPERATION_ABORTED) {
   }
}

How can I achieve that?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Are you looking for some library or function that contains the [System error codes](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx)? or just this linked page? – Sayse Aug 27 '15 at 13:52
  • 1
    A library or function that contains them. – Bart Friederichs Aug 27 '15 at 13:53
  • 2
    [Related: Getting the Windows System Error Code title/description](http://stackoverflow.com/q/1650838/1324033) – Sayse Aug 27 '15 at 13:54
  • @Sayse only marginally, I am looking for constants that make my code more readable, not to format a error message. – Bart Friederichs Aug 27 '15 at 13:57
  • Try looking here: http://www.pinvoke.net/default.aspx/Constants.WINERROR – Icemanind Aug 27 '15 at 13:58
  • 1
    Asking us to look for tools / libraries is off topic on here – deathismyfriend Aug 27 '15 at 14:01
  • @BartFriederichs - I found a couple of other [links](http://stackoverflow.com/questions/3823888/how-do-i-look-up-the-proper-windows-system-error-code-to-use-in-my-application) too by searching for "c# system error codes" but none really seemed to return much useful information. I'm not sure how it can become much cleaner though than what you already have (excluding adding a comment), what are you looking to do inside the if statement? – Sayse Aug 27 '15 at 14:02
  • If you have the Win SDK, you can find them in "winerror.h" – 001 Aug 27 '15 at 14:02
  • @deathismyfriend I was hoping it is part of the C# framework, in the same way the also mentioned `winerror.h` is part of the C SDK. Apparently it isn't. I will not use a library or function to do this. If it isn't available in the framework (which would be a decent answer), I will fix it with a comment. – Bart Friederichs Aug 27 '15 at 14:05

1 Answers1

3

There is nothing built into the .NET Framework. But, you can download the complete error code class from here. It is a C# class that contains the complete list of error codes in C#. It is contained all in one file.

Add this file to your project and you should be able to do something like this:

using Microsoft.Win32.Interop;

...
...

try {
    ...
    ...
} catch (HttpListenerException e) {
    if (e.ErrorCode == WinError.ERROR_OPERATION_ABORTED) {
        // Do something
    }
}

EDIT

The above link seems to be dead now, so I am adding the link to JetBrains version. You can see it here

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • 1
    The included link is dead. Do you know of anywhere that this class is mirrored? – Brian Dec 16 '16 at 16:43
  • @Brian - JetBrains has a pretty good version [here](http://svn.jetbrains.org/omeaopen/trunk/Omea/Src/Core/Interop.WinApi/src/Declarations/Constants/WinError.cs) – Icemanind Dec 16 '16 at 19:45