6

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.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188

1 Answers1

10

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"]);
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    +1, also see http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8efe163b-927e-4895-9983-b8c47b515d7c/ for a good breakdown of this method. – HerrJoebob May 22 '13 at 22:54
  • Thanks @HerrJoebob, now I figured out what i was forgetting. The code could be shortened with simply `mo["CommandLine"]` – Steve May 22 '13 at 23:00