在WMI对象上调用方法时出现InvalidOperationException

时间:2009-10-06 00:10:28

标签: c# wmi

基本上我是WMI的新手,我无法弄清楚我在这里缺少什么,或者如何调试这个......

    ConnectionOptions connectOptions = new ConnectionOptions();
    connectOptions.Username = user;
    connectOptions.Password = password;
    machine = "remoteMachine.com";

    ManagementScope scope = new ManagementScope(
        @"\\" + machine + @"\root\cimv2",
        connectOptions);

    scope.Connect();

    ManagementPath path = new ManagementPath(@"\\" + machine + @"\root\cimv2\Win23_Process");

    ManagementClass proc = new ManagementClass(scope, path, new ObjectGetOptions());

    ManagementBaseObject args = proc.GetMethodParameters("Create");

最后一行,我在proc上调用一个方法,导致这个InvalidOperationException ......并没有告诉我太多:(我在调用proc上的其他方法时得到了相同的结果,比如GetInstances()。通过代码,看起来我可以成功连接到远程机器。

System.InvalidOperationException was unhandled
  Message="Operation is not valid due to the current state of the object."
  Source="System.Management"
  StackTrace:
       at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersClass, IWbemClassObjectFreeThreaded& outParametersClass)
       at System.Management.ManagementObject.GetMethodParameters(String methodName)

1 个答案:

答案 0 :(得分:2)

您的示例代码中有一个拼写错误,不确定它是否在您的实际代码中。你有Win23_Process,它应该是Win32_Process。另外,为Win32_Process创建需要诸如命令行,目录等参数