我有三台远程电脑需要进行远程连接。我正在尝试编写一个简单的Windows应用程序,它将在一个窗口中显示特定进程是否在任何一台机器上运行,例如:
服务器1:Chrome未运行
服务器2:Chrome正在运行
服务器3:Chrome正在运行
我使用了WMI和C#。到目前为止,我已经做到了这么多:
ConnectionOptions connectoptions = new ConnectionOptions();
connectoptions.Username = @"domain\username";
connectoptions.Password = "password";
//IP Address of the remote machine
string ipAddress = "192.168.0.217";
ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
scope.Options = connectoptions;
//Define the WMI query to be executed on the remote machine
SelectQuery query = new SelectQuery("select * from Win32_Process");
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query))
{
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject process in collection)
{
// dwarfs stole the code!! :'(
}
}
我认为一切设置都正确,但如果我在foreach循环中使用MessageBox.Show(process.ToString()),我会得到一堆包含以下文本的消息框:
\\username\root\cimv2:W32_Process.Handle="XXX"
我有些困惑。有没有办法将XXX“翻译”成进程名称?或者,我该如何获取进程的名称,以便使用if语句检查它是否是“chrome”进程?
或者...我的实现是否过于复杂?有更简单的方法来完成这个任务吗?
非常感谢!