C#获取进程用户名

2020-12-17 21:34

阅读:605

标签:class   ntp   rgba   col   span   nbsp   method   dump   ESS   

void Main()
{
    Process.GetCurrentProcess().Id.Dump();
    getProcessOwnerName(Process.GetCurrentProcess().Id).Dump();
}

public string getProcessOwnerName(int processId)
{
    var processes = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ProcessId = " + processId);
    foreach (System.Management.ManagementObject process in processes.Get())
    {
        try
        {            
            string[] OwnerInfo = new string[2];
            process.InvokeMethod("GetOwner", (object[])OwnerInfo);
            return OwnerInfo[0];    
        }
        catch
        {
            return string.Empty;
        }
    }
    
    return string.Empty;
}

 

C#获取进程用户名

标签:class   ntp   rgba   col   span   nbsp   method   dump   ESS   

原文地址:https://www.cnblogs.com/nanfei/p/14004080.html


评论


亲,登录后才可以留言!