在.Net framework中是否有系统错误代码的枚举?
我有一个返回GetLastError代码()的库函数。我需要将它们与特定错误进行比较,例如在.Net framework中是否有系统错误代码的枚举?,.net,pinvoke,.net,Pinvoke,我有一个返回GetLastError代码()的库函数。我需要将它们与特定错误进行比较,例如错误\u无效\u句柄。然而,我自己定义这些常量并不舒服。所以问题是,是否有一个用于此目的的预定义枚举 不,你必须自己做 您可以将Kat推拿添加的代码复制到自己的类SystemErrorCodes中。 它包含从0到499的代码。这是一个很好的开端。如果有人已经有了一个包含所有代码的类,那么他的代码将非常感谢。我为此发布了一个: Install-Package BetterWin32Errors 首先为库添加
错误\u无效\u句柄
。然而,我自己定义这些常量并不舒服。所以问题是,是否有一个用于此目的的预定义枚举 不,你必须自己做
您可以将Kat推拿添加的代码复制到自己的类SystemErrorCodes中。 它包含从0到499的代码。这是一个很好的开端。如果有人已经有了一个包含所有代码的类,那么他的代码将非常感谢。我为此发布了一个:
Install-Package BetterWin32Errors
首先为库添加一个using:
using BetterWin32Errors;
然后你可以这样使用它:
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);
}
}
有关如何使用库的更多示例,请参阅。Win32Exception类可用于此操作
/// <summary>
/// Setting the system Date and Time
/// </summary>
/// <param name="dateAndTime">The date and time settings</param>
/// <returns>Operation success</returns>
public static bool SetSystemTimeAndDate(DateTime dateAndTime)
{
var newDateAndTime = new Win32SystemTimeStruct(dateAndTime);
var ret = Win32ApiStub.SetSystemTime(ref newDateAndTime);
if(!ret)
ThrowExceptionForHr(Marshal.GetLastWin32Error());
return ret;
}
/// <summary>
/// This function checks an error code and throws a nice exception if the code is signifying an error.
/// Do not confuse this with <see cref="Marshal.ThrowExceptionForHR"/> which gets error information out of the context of the current thread.
/// </summary>
/// <param name="nativeErrorCode">The HRESULT error code</param>
public static void ThrowExceptionForHr(int nativeErrorCode)
{
if (nativeErrorCode != 0)
throw new Win32Exception(nativeErrorCode);
}
//
///设置系统日期和时间
///
///日期和时间设置
///手术成功
公共静态布尔设置SystemTimeandDate(日期时间日期和时间)
{
var newDateAndTime=newwin32systemtimestruct(dateAndTime);
var ret=Win32ApiStub.SetSystemTime(参考newDateAndTime);
如果(!ret)
ThroweExceptionForHR(Marshal.GetLastWin32Error());
返回ret;
}
///
///此函数检查错误代码,如果代码表示错误,则抛出一个良好的异常。
///不要将它与从当前线程的上下文中获取错误信息相混淆。
///
///HRESULT错误代码
公共静态无效ThroweExceptionForHR(int nativeErrorCode)
{
如果(nativeErrorCode!=0)
抛出新的Win32Exception(nativeErrorCode);
}
pinvoke发布了一个课程。我还没有试着在这里发布12000行
下载:
使用系统;
使用系统运行时;
使用System.Runtime.InteropServices;
运用系统反思;
命名空间Microsoft.Win32.Interop
{
公共类结果32
{
公共静态字符串GetErrorName(int结果)
{
FieldInfo[]fields=typeof(ResultWin32).GetFields();
foreach(字段中的字段信息fi)
if((int)fi.GetValue(null)=结果)
返回fi.Name;
返回字符串。空;
}
///
///操作已成功完成。
///
public const int ERROR_SUCCESS=0;
///
///功能不正确。
///
public const int ERROR\u INVALID\u FUNCTION=1;
///
///系统找不到指定的文件。
///
public const int ERROR_FILE_NOT_FOUND=2;
///
///系统找不到指定的路径。
///
//!!不要使用此切口
//在以下位置获取整个文件:http://www.pinvoke.net/default.aspx/Constants/WINERROR.html
公共常数int COMADMIN_E_SAFERINVALID=(int)(0x80110822-0x100000000);
///
///指定的用户无法写入系统注册表
///
public const int COMADMIN_E_REGISTRY_ACCESSDENIED=(int)(0x80110823-0x100000000);
///
///没有可访问的信息。
///
public const int COMADMIN_E_PARTITIONS_DISABLED=(int)(0x80110824-0x100000000);
///
///无法打开文件。
///
public const int NS_E_FILE_OPEN_FAILED=(int)(0xC00D001DL-0x100000000);
公共静态bool成功(int结果)
{
返回结果>=0;
}
公共静态bool失败(int结果)
{
返回结果<0;
}
}
}
我的回答最初是针对所描述的错误,但你的答案似乎是其中的一个子集
这里所有的答案都是关于必须手动完成工作。我就是这么做的(删除了前面提到的链接,并用这些值生成了一个enum类)
但是,令我失望的是,我发现您只需执行抛出新的Win32Exception(errorCode)
即可得到匹配的错误消息文本
阅读。哇!真是个救命恩人!我很惊讶,有人花了15年时间才把处理Win32错误代码的简单方法组装起来。谢谢另请参见:和FormatMessage
using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Reflection;
namespace Microsoft.Win32.Interop
{
public class ResultWin32
{
public static string GetErrorName(int result)
{
FieldInfo[] fields = typeof(ResultWin32).GetFields();
foreach (FieldInfo fi in fields)
if ((int)fi.GetValue(null) == result)
return fi.Name;
return String.Empty;
}
/// <summary>
/// The operation completed successfully.
/// </summary>
public const int ERROR_SUCCESS = 0;
/// <summary>
/// Incorrect function.
/// </summary>
public const int ERROR_INVALID_FUNCTION = 1;
/// <summary>
/// The system cannot find the file specified.
/// </summary>
public const int ERROR_FILE_NOT_FOUND = 2;
/// <summary>
/// The system cannot find the path specified.
/// </summary>
// !! Don't use this cutout
// get the whole file at: http://www.pinvoke.net/default.aspx/Constants/WINERROR.html
public const int COMADMIN_E_SAFERINVALID = (int)(0x80110822 - 0x100000000);
/// <summary>
/// The specified user cannot write to the system registry
/// </summary>
public const int COMADMIN_E_REGISTRY_ACCESSDENIED = (int)(0x80110823 - 0x100000000);
/// <summary>
/// No information avialable.
/// </summary>
public const int COMADMIN_E_PARTITIONS_DISABLED = (int)(0x80110824 - 0x100000000);
/// <summary>
/// Failed to open a file.
/// </summary>
public const int NS_E_FILE_OPEN_FAILED = (int)(0xC00D001DL - 0x100000000);
public static bool Succeeded(int result)
{
return result >= 0;
}
public static bool Failed(int result)
{
return result < 0;
}
}
}