locked
How to get the command line parameters from a different process in C# RRS feed

  • Question

  • I have a simple program in C#.  I am trying to get the command line parameters of a process (it was started from a cmd.exe shell).  When I use the Process Explorer on Windows Vista,  I see the command line parameters but when I run this simple program,  it does not print the command line params/agruments.

     

    I have also executed the program with "Run As Administrator" and it still does not print the values.  The question is,  Am I missing something here or is it not possible to get these command line parameters from C# program.

     

    thanks,

     

     

    using System;

    using System.Diagnostics;

    namespace ProcessDetails
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process javaProcess = Process.GetProcessById(6136);
                Console.WriteLine("Process name is {0}", javaProcess.ProcessName);
                ProcessStartInfo javaProcessInfo = javaProcess.StartInfo
                Console.WriteLine(javaProcessInfo.Arguments);
            }
        }
    }

    Friday, February 29, 2008 12:28 AM

Answers

  • AFAIK, there is no fully supported way to do this with the public Windows API (excluding WMI, which I will get to in a second).  This is probably why the Process object does not provide this information.

     

    There is the unsupported NtQueryInformationProcess API: http://msdn2.microsoft.com/en-us/library/ms684280.aspx

     

    However, you can do what you want via the operating system's Windows Management (WMI) features.  One of the things that IS documented there is that the Win32_Process management object provides the CommandLine.  Great!

     

    Reference System.Management.dll in your C# project.  Substitute a valid ProcessId (which you can get through Task Manager or programmatically through System.Diagnostics.Process):

     

    Code Snippet

    using System.Management;

     

    private void WmiTest()

    {

    using (ManagementObjectSearcher mos = new ManagementObjectSearcher(

    "SELECT CommandLine FROM Win32_Process WHERE ProcessId = 1234"))

    {

    foreach (ManagementObject mo in mos.Get())

    {

    Console.WriteLine(mo["CommandLine"]);

    }

    }

    }

     

     

    Friday, February 29, 2008 11:53 PM

All replies

  • AFAIK, there is no fully supported way to do this with the public Windows API (excluding WMI, which I will get to in a second).  This is probably why the Process object does not provide this information.

     

    There is the unsupported NtQueryInformationProcess API: http://msdn2.microsoft.com/en-us/library/ms684280.aspx

     

    However, you can do what you want via the operating system's Windows Management (WMI) features.  One of the things that IS documented there is that the Win32_Process management object provides the CommandLine.  Great!

     

    Reference System.Management.dll in your C# project.  Substitute a valid ProcessId (which you can get through Task Manager or programmatically through System.Diagnostics.Process):

     

    Code Snippet

    using System.Management;

     

    private void WmiTest()

    {

    using (ManagementObjectSearcher mos = new ManagementObjectSearcher(

    "SELECT CommandLine FROM Win32_Process WHERE ProcessId = 1234"))

    {

    foreach (ManagementObject mo in mos.Get())

    {

    Console.WriteLine(mo["CommandLine"]);

    }

    }

    }

     

     

    Friday, February 29, 2008 11:53 PM
  • Thanks BinaryCoder. 

     

    Never thought of looking into WMI for this info.  It would help if Microsoft gave access to this info through the ProcessInfo class.

     

     

     

     

    Saturday, March 1, 2008 2:09 PM
  • I use it in web service but ComandLine property is null
    Thursday, April 12, 2012 11:01 AM