tags:

views:

81

answers:

3

Hello,

How do I get the usernames of all the processes running in task manager using c#?

Thanks

+2  A: 

Look into Win32_Process Class, and GetOwner Method

Sample Code

Sample code

public string GetProcessOwner(int processId) 
{ 
    string query = "Select * From Win32_Process Where ProcessID = " + processId; 
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 
    ManagementObjectCollection processList = searcher.Get(); 

    foreach (ManagementObject obj in processList) 
    { 
        string[] argList = new string[] { string.Empty, string.Empty }; 
        int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)); 
        if (returnVal == 0) 
        { 
            // return DOMAIN\user 
            return argList[1] + "\\" + argList[0]; 
        } 
    } 

    return "NO OWNER"; 
} 
PRR
+1  A: 

You can check this article http://www.codeproject.com/KB/cs/processownersid.aspx

Prakash Kalakoti