进程的用户名

时间:2010-07-23 04:25:35

标签: c# username

如何使用c#?

获取在任务管理器中运行的所有进程的用户名

由于

1 个答案:

答案 0 :(得分:4)

查看Win32_Process ClassGetOwner 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"; 
}