WinForm(C#)根据进程名获取进程的用户名?
//获得进程对象,以用来操作System.Diagnostics.Processmyps=newSystem.Diagnostics.Process();foreach(...
//获得进程对象,以用来操作
System.Diagnostics.Process myps = new System.Diagnostics.Process();
foreach (Process ps in Process.GetProcesses())
{
//获取进程名
listBox1.Items.Add(ps.ProcessName);
//获取用户名,调用方法
//我想在这个循环里面调用一个方法,把进程名传进去,然后根据我传入的进程名返回该进程的用户名,该怎么办??>
}
希望高手能给个代码,谢谢!~!~~!~
2楼大哥: 你的pID是怎么来的啊? 不是进程名称? 展开
System.Diagnostics.Process myps = new System.Diagnostics.Process();
foreach (Process ps in Process.GetProcesses())
{
//获取进程名
listBox1.Items.Add(ps.ProcessName);
//获取用户名,调用方法
//我想在这个循环里面调用一个方法,把进程名传进去,然后根据我传入的进程名返回该进程的用户名,该怎么办??>
}
希望高手能给个代码,谢谢!~!~~!~
2楼大哥: 你的pID是怎么来的啊? 不是进程名称? 展开
展开全部
需要添蚂腔耐加对 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;
}
进程名 是 p.ProcessName
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;
}
进程名 是 p.ProcessName
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ps.StartInfo.UserName
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询