C#获取进程对应的用户名
Posted an7800666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#获取进程对应的用户名相关的知识,希望对你有一定的参考价值。
static void Main(string[] args) foreach (Process p in Process.GetProcesses()) Console.Write(p.ProcessName); Console.Write("----"); Console.WriteLine(GetProcessUserName(p.Id)); Console.ReadKey();
private static string GetProcessUserName(int pID) string userName = string.Empty; try foreach (ManagementObject item in new ManagementObjectSearcher("Select * from Win32_Process WHERE processID=" + pID).Get()) ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; inPar = item.GetMethodParameters("GetOwner"); outPar = item.InvokeMethod("GetOwner", inPar, null); userName = Convert.ToString(outPar["User"]); break; catch userName = "SYSTEM"; return userName;
转载于:https://www.cnblogs.com/liugh/articles/6790691.html
以上是关于C#获取进程对应的用户名的主要内容,如果未能解决你的问题,请参考以下文章