4

I want to know the user that created each process.

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

Amit Joshi
  • 14,103
  • 20
  • 72
  • 131
Josh
  • 13,120
  • 28
  • 112
  • 158

1 Answers1

4

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"; 
} 
Community
  • 1
  • 1
PRR
  • 1,160
  • 7
  • 13