我正在使用this stackoverflow post中的以下C#代码。
ManagementScope oScope = new ManagementScope("\\\\localhost\\root\\default");
ManagementPath oPath = new ManagementPath("SystemRestore");
ObjectGetOptions oGetOp = new ObjectGetOptions();
ManagementClass oProcess = new ManagementClass(oScope, oPath, oGetOp);
ManagementBaseObject oInParams =
oProcess.GetMethodParameters("CreateRestorePoint");
oInParams["Description"] = "Setting restore point";
oInParams["RestorePointType"] = 12; // MODIFY_SETTINGS
oInParams["EventType"] = 100;
ManagementBaseObject oOutParams =
oProcess.InvokeMethod("CreateRestorePoint", oInParams, null);
错误来自声明oOutParams
的行。
我已将此行包装在try catch中,并打印出错误代码-2147023838,该错误代码在列出的here的任何COM错误代码列表中都找不到(假设应该转换打印的错误代码十六进制2的补码-> 0x80070422)。
在没有捕获异常的情况下(因此基本上是上面的确切代码),堆栈跟踪为:
Unhandled Exception: System.Runtime.InteropServices.COMException
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
at WindowsUpdatesDev.Program.Main(String[] args)
将try / catch包裹在声明oOutParams的行周围,然后打印堆栈跟踪:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
at WindowsUpdatesDev.Program.Main(String[] args)
第二条堆栈跟踪没有消息。