gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:42 25 4
gpt4 key购买 nike

如何获取另一个进程的命令行参数?

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

关于c# - 如何在 C# 中读取另一个进程的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com