February 8, 2011

Finding CommandLines of Other Processes

Add a reference to "System.Management" assembly
using System.Management;
...

static void FindAllProcessNameWithCommandLine()
{
    string wmiQuery = "Select * from Win32_Process";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
    ManagementObjectCollection retObjectCollection = searcher.Get();
    foreach (ManagementObject retObject in retObjectCollection)
    {
        string exename = retObject["Name"] as string;
        string commandline = retObject["CommandLine"] as string;
        Debug.WriteLine(exename + ": \'" + commandline + "\'");
    }
}

Sample Output:
...
procexp64.exe: '"C:\Program Files (x86)\Tools\ProcessExplorer\procexp.exe" '
explorer.exe: '"C:\Windows\explorer.exe" /n,/select,"C:\Users\RBovilll\AppData\Local\Temp\Kabo.zip"'
SMSCliUI.exe: 'C:\Windows\SysWOW64\CCM\SMSCliUI.exe -Embedding'
devenv.exe: '"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" '
splwow64.exe: 'splwow64'
...

No comments: