首页  >  文章  >  后端开发  >  C# 获取系统进程的用户名

C# 获取系统进程的用户名

黄舟
黄舟原创
2016-12-22 13:58:451786浏览

需要添加对 System.Management.dll 的引用 

using System.Diagnostics; 
using System.Management;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 text1 = null; 
SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE processID=" + pID); 
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1); 
try 
{ 
foreach (ManagementObject disk in searcher1.Get()) 
{ 
ManagementBaseObject inPar = null; 
ManagementBaseObject outPar = null; 
inPar = disk.GetMethodParameters("GetOwner"); 
outPar = disk.InvokeMethod("GetOwner", inPar, null); 
text1 = outPar["User"].ToString(); 
break; 
} 
} 
catch 
{ 
text1 = "SYSTEM"; 
} 
return text1; 
}

 以上就是C# 获取系统进程的用户名的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn