如何查找要在我的应用程序中使用的正确 Windows 系统错误代码?

Jim*_*ell 3 c# windows error-code

我正在编写一个 C# .NET 2.0 应用程序,其中当预期通过SerialPort. 如果未收到帧(即超时)或确定为无效,我需要使用SetLastError. Windows 有大量的错误代码。是否有简单的工具或参考可以帮助缩小要使用的正确错误代码的范围?

附加信息

虽然抛出异常并在堆栈的更高层处理它是我的偏好,但在这种情况下这不是一个选项,因为我正在更新的应用程序不是为了利用这样一个有用的功能而设计的。

rbo*_*boy 5

不幸的是,以上对我不起作用,但是这对我来说非常有用,粘贴整个代码以便它可以直接复制粘贴到 C# 中

public static class WinErrors
{
    /// <summary>
    /// Gets a user friendly string message for a system error code
    /// </summary>
    /// <param name="errorCode">System error code</param>
    /// <returns>Error string</returns>
    public static string GetSystemMessage(uint errorCode)
    {
        var exception = new Win32Exception((int)errorCode);
        return exception.Message;
    }
}
Run Code Online (Sandbox Code Playgroud)