Is there an enumeration for system error codes in .Net framework?

No, you'll have to make your own.


You can copy the code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx added by katmassage to your own class SystemErrorCodes. It contains the codes from 0 to 499. This is a good starter. If someone already has a class containing also all the codes his code would be appreciated.


I published a NuGet package for this:

Install-Package BetterWin32Errors

First add a using for the library:

using BetterWin32Errors;

Then you can use it like this:

if (!SomeWin32ApiCall(...))
{
    var error = Win32Exception.GetLastWin32Error();
    if (error == Win32Error.ERROR_FILE_NOT_FOUND)
    {
        // ...do something...
    }
    else if (error == Win32Error.ERROR_PATH_NOT_FOUND)
    {
        // ...do something else...
    }
    else
    {
        throw new Win32Exception(error);
    }
}

See the site for more examples of how to use the library.

Tags:

.Net

Pinvoke