2012-12-18 110 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; 
    } 
} 
相關問題