进程是否在远程机器上运行? [英] Is a process running on a remote machine?
问题描述
我有三台远程连接的远程 PC.我正在尝试编写一个简单的 Windows 应用程序,该应用程序将在单个窗口中显示特定进程是否正在任一机器上运行,例如
I have three remote PC's to which I remotely connect. I am trying to write a simple Windows application that would display in a single window whether a particular process is running on either of the machines, e.g.
Server1:Chrome 未运行
Server1: Chrome not running
Server2:Chrome 正在运行
Server2: Chrome IS running
Server3:Chrome 正在运行
Server3: Chrome IS running
我使用了 WMI 和 C#.到目前为止,我有这么多:
I used WMI and C#. So far I've got this much:
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()),我会得到一大堆带有以下文本的消息框:
I think it is all set up correctly, but if I MessageBox.Show(process.ToString()) inside the foreach loop, I get a whole bunch of message boxes with the following text:
\\username\root\cimv2:W32_Process.Handle="XXX"
我有点卡住了.有什么方法可以将 XXX翻译"为进程名称?否则,如何实际获取进程的名称,以便我可以使用 if 语句来检查它是否是chrome"进程?
I am kind of stuck. Is there any way I can "translate" that XXX to a process name? Or else, how can actually get the names of the processes so I can use an if statement to check whether it is a "chrome" process?
或者……我的实现是不是有点矫枉过正?有没有更简单的方法来实现这一点?
Or...is my implementation an overkill? Is there an easier way to accomplish this?
非常感谢!
推荐答案
在你的 foreach 中,试试这个:
In your foreach, try this:
Console.WriteLine(process["Name"]);
这篇关于进程是否在远程机器上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!