获取进程的“命令行"和来自 Process 对象的参数? [英] Get Process's "Command Line" and arguments from Process object?

查看:39
本文介绍了获取进程的“命令行"和来自 Process 对象的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Win7 任务管理器中,有一个名为命令行"的列可以显示,它将准确显示进程是如何启动的,以及所有发出的参数.如果我没有启动当前正在运行的进程的 Process 对象,我如何获取该信息?我曾希望我可以做一些类似 p.StartInfo.Arguments 的事情,但它总是作为一个空字符串返回.整个 StartInfo 属性似乎是空的,可能是因为我没有启动我正在查询的进程.我猜我将不得不使用 WinAPI 调用.

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. If I have a Process object for a currently running process that I did not start, how can I get that information? I had hoped that I could do something like p.StartInfo.Arguments but that's always coming back as an empty string. The entire StartInfo property seems empty, probably because I did not start the process I'm querying. I'm guessing that I'm going to have to use a WinAPI call.

推荐答案

嗯,你可以使用 WMI,有一个可以查询的类来检索进程列表,每个对象还包含一个启动命令行的属性过程

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

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

这篇关于获取进程的“命令行"和来自 Process 对象的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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