Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get username of process running in task manager?

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#?

like image 688
Josh Avatar asked Oct 23 '25 16:10

Josh


1 Answers

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"; 
} 
like image 83
PRR Avatar answered Oct 26 '25 04:10

PRR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!