尝试使用C#创建系统还原点时出现System.Runtime.InteropServices.COMException

时间:2018-12-05 18:31:56

标签: c# wmi windows-10-iot-enterprise

我正在使用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)

第二条堆栈跟踪没有消息。

1 个答案:

答案 0 :(得分:0)

遵循步骤here(具体而言,将我的C:驱动器的“保护”设置为“开”)进行步骤1和步骤2的所有操作)可以使上面的代码正确运行。

相关问题