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
  • 15,448
  • 21
  • 77
  • 141
Josh
  • 13,530
  • 29
  • 114
  • 159

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,190
  • 7
  • 13