Corey Ogburn
Corey Ogburn

Reputation: 24717

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. 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.

Upvotes: 6

Views: 10112

Answers (1)

Steve
Steve

Reputation: 216293

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

Upvotes: 11

Related Questions