如何阅读在C#中的另一个进程的命令行参数? [英] How to read command line arguments of another process in C#?
问题描述
我怎样才能获得另一个进程的命令行参数?
使用的System.Diagnostics.Process
类的静态功能,我可以获取正在运行的进程,例如列表按名称:
过程[] = PROCESSLIST Process.GetProcessesByName(processName);
然而,没有办法来访问用于启动这一进程的命令行。一会怎么做呢?
如果您没有使用Start方法来启动一个过程,StartInfo的属性不会反映用于启动该流程的参数。例如,如果您使用GetProcesses获取的计算机上运行的进程数组,每个过程的StartInfo的属性不包含用于启动过程中的原始文件名或参数。 (来源:<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.process.startinfo.aspx\">MSDN)
块引用>斯图尔特的WMI的建议是一个很好的:
字符串wmiQuery =的String.Format(从Win32_Process的选择,其中的CommandLine名='{0}',processName);
ManagementObjectSearcher搜索=新ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
的foreach(的ManagementObject retObject在retObjectCollection)
Console.WriteLine([{0}],retObject [的CommandLine]);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: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"]);
这篇关于如何阅读在C#中的另一个进程的命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!