我有三台远程PC,我可以远程连接。我正在尝试编写一个简单的Windows应用程序,该应用程序可以在单个窗口中显示特定进程是否在任一台计算机上运行,例如,进程是否在远程机器上运行?
服务器1:浏览器没有运行
服务器2:在Chrome运行
Server3的:在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!! :'(
}
}
,我认为它是所有设置正确,但如果我的MessageBox.show(process.ToString())foreach循环中,我得到了一大堆消息以下文字的框:
\\username\root\cimv2:W32_Process.Handle="XXX"
我有点卡住了。有什么方法可以将XXX翻译成进程名称?否则,如何才能真正获取进程的名称,以便我可以使用if语句来检查它是否是“chrome”进程?
或...是我的执行过度了吗?有没有更简单的方法来实现这一点?
非常感谢!
我在哪里可以找到某种类型的属性列表,如“名称”?它的作品,只是不知道你从哪里得到它.. – Krzysiek
好问题 - 必须有一个列表的地方。 IIRC,我最初从CodeProject.com上的例子中得到了这个。 –
MSDN文档中列出了Win32_Process WMI类的属性http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx – RRUZ