2012-12-18 71 views
8

我已经尝试了两种方法来完成此目的。如何获取远程计算机上正在运行的进程的描述?

第一种方法,我用System.Diagnostics,但我得到NotSupportedException的“功能不支持远程机器”的MainModule

foreach (Process runningProcess in Process.GetProcesses(server.Name)) 
{ 
    Console.WriteLine(runningProcess.MainModule.FileVersionInfo.FileDescription); 
} 

第二种方式,我试图用System.Management但似乎ManagementObjectDescription是她同为Name

string scope = @"\\" + server.Name + @"\root\cimv2"; 
string query = "select * from Win32_Process"; 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
ManagementObjectCollection collection = searcher.Get(); 

foreach (ManagementObject obj in collection) 
{ 
    Console.WriteLine(obj["Name"].ToString()); 
    Console.WriteLine(obj["Description"].ToString()); 
} 

是否有人会知道更好的方法来获取远程计算机上正在运行的进程的描述?

+0

您是否尝试过使用Rob van der Woude的wmigen?它可能有助于显示可用的内容。 http://www.robvanderwoude.com/wmigen.php – Lizz

+0

@Lizz那么我已经尝试循环obj的属性,并检查Property.ToString()是否包含应该在描述中的关键字我正在寻找的过程之一... – athom

+0

哎呀。对不起,想不到别的。 :(这很有趣 - 而且很奇怪。+ +1代码和故障排除功能!:) – Lizz

回答

4

嗯,我想我已经有了一个方法来做到这一点,这对我的目的来说足够好。我基本上从ManagementObject获取文件路径,并从实际文件中获取描述。

ConnectionOptions connection = new ConnectionOptions(); 
connection.Username = "username"; 
connection.Password = "password"; 
connection.Authority = "ntlmdomain:DOMAIN"; 

ManagementScope scope = new ManagementScope(@"\\" + serverName + @"\root\cimv2", connection); 
scope.Connect(); 

ObjectQuery query = new ObjectQuery("select * from Win32_Process"); 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
ManagementObjectCollection collection = searcher.Get(); 

foreach (ManagementObject obj in collection) 
{ 
    if (obj["ExecutablePath"] != null) 
    { 
     string processPath = obj["ExecutablePath"].ToString().Replace(":", "$"); 
     processPath = @"\\" + serverName + @"\" + processPath; 

     FileVersionInfo info = FileVersionInfo.GetVersionInfo(processPath); 
     string processDesc = info.FileDescription; 
    } 
} 
相关问题