如何获取另一个进程的命令行参数?
使用 System.Diagnostics.Process
类的静态函数,我可以获得正在运行的进程列表,例如按名称:
Process[] processList = Process.GetProcessesByName(processName);
但是,无法访问用于启动此进程的命令行。如何做到这一点?
If you did not use the Start method to start a process, the StartInfo property does not reflect the parameters used to start the process. For example, if you use GetProcesses to get an array of processes running on the computer, the StartInfo property of each Process does not contain the original file name or arguments used to start the process. (source: MSDN)
Stuart 的 WMI 建议很好:
string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
foreach (ManagementObject retObject in retObjectCollection)
Console.WriteLine("[{0}]", retObject["CommandLine"]);
我是一名优秀的程序员,十分优秀!