鳕鱼天空

This is Mr Wang's Tech Blog.

[转]C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )

private static IEnumerable<string> GetCommandLines(string processName)
{
    List<string> results = new List<string>();
    string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName);
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery))
    {
using (ManagementObjectCollection retObjectCollection = searcher.Get())
{
    foreach (ManagementObject retObject in retObjectCollection)
    {
results.Add((string)retObject["CommandLine"]);
    }
}
    }
    return results;
}
static void Main(string[] args)
{
    var result = GetCommandLines("msvsmon.exe");
    Console.Read();
}

任务管理器中实际的参数如下, 该程序或得到3个item的string。

  • WMI的C++例子:
    http://msdn.microsoft.com/zh-cn/aa394558

    http://msdn.microsoft.com/zh-cn/aa389762