简体   繁体   English

从Process对象获取Process的“命令行”和参数?

[英]Get Process's “Command Line” and arguments from Process object?

In my Win7 Task Manager, there's a column that can be displayed called "Command Line" and will show exactly how the process was started and all the parameters issued. 在我的Win7任务管理器中,有一个可以显示的名为“命令行”的列,它将准确显示进程的启动方式以及发出的所有参数。 If I have a Process object for a currently running process that I did not start, how can I get that information? 如果我没有启动当前正在运行的进程的Process对象,我该如何获取该信息? I had hoped that I could do something like p.StartInfo.Arguments but that's always coming back as an empty string. 我曾经希望我可以做一些像p.StartInfo.Arguments这样的东西但是它总是以空字符串的形式返回。 The entire StartInfo property seems empty, probably because I did not start the process I'm querying. 整个StartInfo属性似乎是空的,可能是因为我没有启动我正在查询的进程。 I'm guessing that I'm going to have to use a WinAPI call. 我猜我将不得不使用WinAPI调用。

Well you could use WMI, there is a class that could be queryied to retrieve the process list and each object contains also a property for the command line that started the process 那么你可以使用WMI,有一个类可以被查询以检索进程列表,每个对象也包含启动进程的命令行的属性

string query = "SELECT Name, CommandLine, ProcessId, Caption, ExecutablePath " + 
               "FROM Win32_Process";
string wmiScope = @"\\your_computer_name\root\cimv2";
ManagementObjectSearcher searcher = new ManagementObjectSearcher (wmiScope, query);
foreach (ManagementObject mo in searcher.Get ()) 
{
    Console.WriteLine("Caption={0} CommandLine={1}", 
             mo["Caption"], mo["CommandLine"]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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