2012-06-01 54 views
7

我有三台远程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”进程?

或...是我的执行过度了吗?有没有更简单的方法来实现这一点?

非常感谢!

回答

6

在你的foreach,试试这个:

Console.WriteLine(process["Name"]); 
+1

我在哪里可以找到某种类型的属性列表,如“名称”?它的作品,只是不知道你从哪里得到它.. – Krzysiek

+0

好问题 - 必须有一个列表的地方。 IIRC,我最初从CodeProject.com上的例子中得到了这个。 –

+3

MSDN文档中列出了Win32_Process WMI类的属性http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx – RRUZ

2

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

定义中的System.Diagnostics.Process作为

public static Process[] GetProcessesByName(
    string processName, 
    string machineName) 
+0

这可能是一个新手问题,但是如何指定远程机器的computerName?也就是说,在哪里指定IP,用户名,密码......? – Krzysiek

+0

你不知道你想连接的机器名称?您需要知道名称和/或IP地址。您还需要能够以管理员身份从监控计算机登录到受监控的计算机。 –

+1

我不能说(“chrome”,“ipnumber”)..我在哪里提供用户名/密码? – Krzysiek

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(); 
     } 
    } 
} 
相关问题