调用 WMI 对象上的方法时出现 InvalidOperationException
基本上我是 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...也没有告诉我much :( 在 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)
Basically I am new to WMI and I can't figure out what I am missing here or how to debug this...
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");
That last line, where I call a method on proc, causes this InvalidOperationException... doesn't tell me too much :( I get the same result when calling other methods on proc, such as GetInstances(). When debugging through the code, it does appear that I can connect to the remote machine successfully.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例代码中有一个拼写错误,不确定它是否在您的真实代码中。您已经得到了 Win23_Process,它应该是 Win32_Process。 Create for Win32_Process 还需要命令行、目录等参数。
You've got a typo in your sample code, not sure if it's in your real code or not. You've got Win23_Process, which should be Win32_Process. Also Create for Win32_Process needs arguments such as command-line, directory, etc.