none
How to: Get the Command Line Arguments of running Processes RRS feed

  • Question

  • Assuming i get a list of all local processes in C#:
    // Get all processes running on the local computer.
    Process [] localAll = Process.GetProcesses();
    How can i get the arguments that have been passed to these processes on start?

    In Unix even ps command does show arguments. How do i get this information in Windows using .Net 2.0?

    Cheers Alex
    Tuesday, July 22, 2008 7:58 AM

Answers

  • I think that the StartInfo class is only used when you are starting an application.

    The easiest way (I think) to do what you are attempting to do is to use the Win32_Process WMI class.  This has a CommandLine property.

                ManagementClass mngmtClass = new ManagementClass("Win32_Process");  
                foreach (ManagementObject o in mngmtClass.GetInstances())  
                {  
                    if (o["Name"].Equals("notepad.exe"))  
                    {  
                        Console.WriteLine(o["Name"] + " [" + o["CommandLine"] + "]");  
                    }  
                } 

    This returns something like:

    "C:\Program Files\Windows NT\Accessories\NOTEPAD.EXE" c:\general.log

    You should be able to optimize by looking for the specific process as well.

    Michael Fischer

    • Edited by _Michael Fischer_ Tuesday, July 22, 2008 8:35 PM fixed some typos
    • Marked as answer by fu.7 Wednesday, July 23, 2008 1:03 PM
    Tuesday, July 22, 2008 6:40 PM

All replies

  • Process.StartInfo.Arguments should do the job.
    Tuesday, July 22, 2008 10:08 AM
  • I am using Windows XP with .Net Framework 2.0.

    Lets start notepad with argument "C:\tmp\blah.txt" in PowerShell:
    PS C:\tmp> $p = New-Object System.Diagnostics.Process; ${p}.StartInfo.FileName = "notepad"; ${p}.StartInfo.Arguments
     = "C:\tmp\blah.txt"; ${p}.Start()

    Lets compile and run this C# program afterwards:
    using System; 
    using System.Diagnostics; 

    namespace ProcessArgsTest 
      class Test 
      { 
        static void Main()  
        { 
          Process [] localAll = Process.GetProcesses(); 
          foreach (Process p in localAll) 
          {   
            if (p.ProcessName == "notepad"
            { 
              Console.WriteLine(p.ProcessName + " [" + p.StartInfo.Arguments + "]"); 
            } 
          } 
        } 
      } 


    Lets have a look at the outupt:
    notepad []

    Please can you give me an example how Process.StartInfo.Arguments should do the job?

    Cheers Alex

    Edit:
    Of cause we can do it oldschool as well with same result:
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Dokumente und Einstellungen\alex>notepad C:\tmp\blah.txt
    • Edited by fu.7 Tuesday, July 22, 2008 11:16 AM additional info
    Tuesday, July 22, 2008 10:46 AM
  • I think that the StartInfo class is only used when you are starting an application.

    The easiest way (I think) to do what you are attempting to do is to use the Win32_Process WMI class.  This has a CommandLine property.

                ManagementClass mngmtClass = new ManagementClass("Win32_Process");  
                foreach (ManagementObject o in mngmtClass.GetInstances())  
                {  
                    if (o["Name"].Equals("notepad.exe"))  
                    {  
                        Console.WriteLine(o["Name"] + " [" + o["CommandLine"] + "]");  
                    }  
                } 

    This returns something like:

    "C:\Program Files\Windows NT\Accessories\NOTEPAD.EXE" c:\general.log

    You should be able to optimize by looking for the specific process as well.

    Michael Fischer

    • Edited by _Michael Fischer_ Tuesday, July 22, 2008 8:35 PM fixed some typos
    • Marked as answer by fu.7 Wednesday, July 23, 2008 1:03 PM
    Tuesday, July 22, 2008 6:40 PM
  • Thank you a lot!

    I will do it like this:
    using System; 
    using System.Management; 
    using System.Text.RegularExpressions; 
     
    namespace ProcessArgsTest 
      class Test 
      { 
        static void Main()  
        { 
          ManagementClass mngmtClass = new ManagementClass("Win32_Process"); 
          foreach (ManagementObject o in mngmtClass.GetInstances()) 
          { 
            if (o["Name"].Equals("javaw.exe")) 
            { 
              String commandLine = (String) o["CommandLine"]; 
              Regex envRE = new Regex("-environment ([^ ]+) "); 
              Match m = envRE.Match(commandLine); 
              if (m.Success) { 
                Console.WriteLine(o["Name"] + " [" + m.Groups[1] + "]"); 
              } 
            } 
          } 
        } 
      } 
     


    With that code like that i can identify which java process is related to which environment (we have lots of java processes and the environment is specified by an argument).

    KR Alex

    Wednesday, July 23, 2008 1:14 PM