ManagementObjectSearcher Get() 方法 returns 没有结果
ManagementObjectSearcher Get() method returns no results
我正在尝试使用 .NET 4.5 上的 WMI/C# 终止远程计算机上的进程。在下面的代码中,当在 ManagementObjectSearcher 实例上调用 Get 方法时,不会返回任何内容,因此未到达 foreach 内的行。 ManagementScope 已连接,查询变量包含要终止的进程的名称。
感谢您的帮助。
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\{0}\ROOT\CIMV2", NetworkName), connOptions);
manScope.Connect();
var query = new SelectQuery("select * from Win32_process where name = '" + ProcessName + "'");
using (var searcher = new ManagementObjectSearcher(manScope, query))
{
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null);
}
}
}
catch (ManagementException err)
{
//Do something with error message here
}
使用Count
属性检查,是否包含任何记录。也就是说,if(searcher.Get().count == 0)
returns true
,表示没有记录。
正如我在上面的评论中所概述的,为了完整起见,我对代码进行了如下更改。
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\{0}\ROOT\CIMV2", NetworkName), connOptions);
manScope.Connect();
ProcessName = ProcessName + ".exe";
using (var searcher = new ManagementObjectSearcher(manScope, new SelectQuery("select * from Win32_Process where Name = '" + ProcessName + "'")))
{
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null);
}
}
}
catch (ManagementException err)
{
//Do something with error message here
}
在我的例子中,我无法使用 WMI 查询远程接收 CPU 利用率值:
SELECT PercentProcessorTime FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'
我将项目构建平台目标从 Any CPU 更改为 x64 以匹配我的系统位数,问题已解决.另一种方法是在选择 Any CPU 时取消选中 Prefer 32-bit 复选框。
我正在尝试使用 .NET 4.5 上的 WMI/C# 终止远程计算机上的进程。在下面的代码中,当在 ManagementObjectSearcher 实例上调用 Get 方法时,不会返回任何内容,因此未到达 foreach 内的行。 ManagementScope 已连接,查询变量包含要终止的进程的名称。 感谢您的帮助。
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\{0}\ROOT\CIMV2", NetworkName), connOptions);
manScope.Connect();
var query = new SelectQuery("select * from Win32_process where name = '" + ProcessName + "'");
using (var searcher = new ManagementObjectSearcher(manScope, query))
{
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null);
}
}
}
catch (ManagementException err)
{
//Do something with error message here
}
使用Count
属性检查,是否包含任何记录。也就是说,if(searcher.Get().count == 0)
returns true
,表示没有记录。
正如我在上面的评论中所概述的,为了完整起见,我对代码进行了如下更改。
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\{0}\ROOT\CIMV2", NetworkName), connOptions);
manScope.Connect();
ProcessName = ProcessName + ".exe";
using (var searcher = new ManagementObjectSearcher(manScope, new SelectQuery("select * from Win32_Process where Name = '" + ProcessName + "'")))
{
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null);
}
}
}
catch (ManagementException err)
{
//Do something with error message here
}
在我的例子中,我无法使用 WMI 查询远程接收 CPU 利用率值:
SELECT PercentProcessorTime FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'
我将项目构建平台目标从 Any CPU 更改为 x64 以匹配我的系统位数,问题已解决.另一种方法是在选择 Any CPU 时取消选中 Prefer 32-bit 复选框。