C# 获取系统进程的用户名
C# 获取系统进程的用户名
发布时间:2016-12-28 来源:查字典编辑
摘要:需要添加对System.Management.dll的引用复制代码代码如下:usingSystem.Diagnostics;usingSys...

需要添加对 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#教程学习
热门C#教程学习
编程开发子分类