【问题标题】:System.Runtime.InteropServices.COMException while trying to create a system restore point using C#尝试使用 C# 创建系统还原点时出现 System.Runtime.InteropServices.COMException
【发布时间】:2023-03-04 10:14:01
【问题描述】:

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

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

【问题讨论】:

  • 你有完整的堆栈跟踪吗?让我们看看吧。
  • 以及内部异常的堆栈跟踪,如果有的话。
  • @RobertHarvey 我添加了运行上述代码时打印的堆栈跟踪,以及内部 try/catch 的堆栈跟踪。
  • 关于错误代码的信息是here服务无法启动,要么是因为它被禁用,要么是因为它没有与之关联的启用设备。

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


【解决方案1】:

按照here 的步骤(具体而言,将我的 C: 驱动器的“保护”设置为“开启”的所有步骤 1 和步骤 2)允许上面的代码无错误地运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多