7

我有三台远程连接的远程 PC。我正在尝试编写一个简单的 Windows 应用程序,该应用程序将在单个窗口中显示特定进程是否在任何一台机器上运行,例如

Server1:Chrome 未运行

Server2: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!! :'(                        
                }
            }

我认为这一切都设置正确,但是如果我在 foreach 循环中使用 MessageBox.Show(process.ToString()) ,我会得到一大堆带有以下文本的消息框:

\\username\root\cimv2:W32_Process.Handle="XXX"

我有点卡住了。有什么办法可以将 XXX“翻译”为进程名称?或者,如何才能真正获得进程的名称,以便我可以使用 if 语句来检查它是否是“chrome”进程?

或者......我的实现是不是有点矫枉过正?有没有更简单的方法来实现这一点?

非常感谢!

4

3 回答 3

7

在你的 foreach 中,试试这个:

Console.WriteLine(process["Name"]);
于 2012-06-01T16:55:58.530 回答
3

您可以在 WQL 语句中过滤要监视的进程的名称,因此您可以编写类似这样的内容

 SelectQuery query = new SelectQuery("select * from Win32_Process Where Name='Chrome.exe'");

试试这个示例应用

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='Chrome.exe'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);


                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                  //for each instance found, do something  
                  Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
于 2012-06-01T16:54:14.300 回答
2

试试Process.GetProcesses("chrome", "computerName")

在 System.Diagnostics.Process 中定义为

public static Process[] GetProcessesByName(
   string processName,
   string machineName)
于 2012-06-01T16:43:39.223 回答