【问题标题】:How to get the description of a running process on a remote machine?如何获取远程机器上正在运行的进程的描述?
【发布时间】:2012-12-06 05:41:29
【问题描述】:

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

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

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

第二种方式,我尝试使用System.Management,但似乎ManagementObjectDescriptionName 相同。

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

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

【问题讨论】:

  • 您是否尝试过使用 Rob van der Woude 的 wmigen?它可能有助于显示可用的内容。 robvanderwoude.com/wmigen.php
  • @Lizz 好吧,我已经尝试循环遍历 obj 的属性并检查 Property.ToString() 是否包含一个关键字,该关键字应该在我的一个进程的描述中正在寻找...
  • 哎呀。抱歉,想不到别的了。 :( 这很有趣 - 也很奇怪。+1 以获得良好的代码和故障排除!:)

标签: c# process wmi remote-access


【解决方案1】:

嗯,我想我有一种方法可以很好地满足我的目的。我基本上是从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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多