Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
posts - 96, comments - 70, trackbacks - 0

Using WMI to fetch the command line that started all instances of a process

/// <summary>
/// Using WMI to fetch the command line that started all instances of a process
/// </summary>
/// <param name="processName">Image name, e.g. WebDev.WebServer.exe</param>
/// <returns></returns>
/// adapted from http://stackoverflow.com/questions/504208/how-to-read-command-line-arguments-of-another-process-in-c/504378%23504378
/// original code by http://stackoverflow.com/users/61396/xcud
private static IEnumerable<string> GetCommandLines(string processName)
{
    List<string> results = new List<string>();

    string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName);

    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery))
    {
        using (ManagementObjectCollection retObjectCollection = searcher.Get())
        {
            foreach (ManagementObject retObject in retObjectCollection)
            {
                results.Add((string)retObject["CommandLine"]);
            }
        }
    }
    return results;
}


Technorati tags: , ,

Print | posted on Sunday, April 11, 2010 2:12 PM | Filed Under [ CodeProject-Tip ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 2 and type the answer here:

Powered by: