如何在 C# 中读取另一个进程的命令行参数? [英] How to read command line arguments of another process in C#?

查看:30
本文介绍了如何在 C# 中读取另一个进程的命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

使用 System.Diagnostics.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?

推荐答案

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

Stuart 的 WMI 建议很好:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆