如何在C#中读取另一个进程的命令行参数?

时间:2022-09-20 20:57:17

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, e.g. by name:

使用System.Diagnostics.Process类的静态函数,我可以获得正在运行的进程列表,例如按名字:

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?

但是,无法访问用于启动此过程的命令行。怎么会那样做?

4 个解决方案

#1


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)

如果未使用Start方法启动进程,则StartInfo属性不会反映用于启动进程的参数。例如,如果使用GetProcesses获取计算机上运行的进程数组,则每个进程的StartInfo属性不包含用于启动进程的原始文件名或参数。 (来源:MSDN)

Stuart's WMI suggestion is a good one:

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

#2


If you're targeting Windows XP or later and you can afford the overhead of WMI, a possibility would be to look up the target process using WMI's WIN32_Process class, which has a CommandLine property.

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

#3


Process.StartInfo returns a ProcessStartInfo object that allegedly but not necessarily has the arguments in the Arguments property.

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

#4


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?

这两个项目都是你的吗?您是否可以修改您尝试检查的进程的源,以使其为您提供命令行参数,而不是尝试从该进程之外的某个位置读取它们?

#1


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)

如果未使用Start方法启动进程,则StartInfo属性不会反映用于启动进程的参数。例如,如果使用GetProcesses获取计算机上运行的进程数组,则每个进程的StartInfo属性不包含用于启动进程的原始文件名或参数。 (来源:MSDN)

Stuart's WMI suggestion is a good one:

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

#2


If you're targeting Windows XP or later and you can afford the overhead of WMI, a possibility would be to look up the target process using WMI's WIN32_Process class, which has a CommandLine property.

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

#3


Process.StartInfo returns a ProcessStartInfo object that allegedly but not necessarily has the arguments in the Arguments property.

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

#4


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?

这两个项目都是你的吗?您是否可以修改您尝试检查的进程的源,以使其为您提供命令行参数,而不是尝试从该进程之外的某个位置读取它们?