8

到目前为止,我已经尝试了两种方法来实现这一点。

第一种方式,我用过System.Diagnostics,但NotSupportedExceptionMainModule.

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

第二种方式,我尝试使用System.Management,但似乎DescriptionManagementObjectName.

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());
}

有人会碰巧知道一种更好的方法来获取远程机器上正在运行的进程的描述吗?

4

1 回答 1

5

好吧,我认为我有一种方法可以很好地满足我的目的。我基本上是从文件中获取文件路径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;
    }
}
于 2013-01-07T19:59:14.767 回答