简体   繁体   中英

How to read command line arguments of another process in C#?

How can I obtain the command line arguments of another process?

Using static functions of the System.Diagnostics.Process class I can obtain a list of running processes, eg by name:

Process[] processList = Process.GetProcessesByName(processName);

However, there is no way to access the command line used to start this process. How would one do that?

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's WMI suggestion is a good one:

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"]);

如果您的目标是Windows XP或更高版本,并且可以负担WMI的开销,则可能是使用WMI的WIN32_Process类 (具有CommandLine属性)查找目标进程。

Process.StartInfo返回一个ProcessStartInfo对象,该对象据称但不一定在Arguments属性中具有参数。

Are both projects yours? Could you modify the source for the process you're trying to examine to make it give you its command-line arguments, rather than trying to read them from somewhere outside of that process?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM