我如何找出以编程方式拥有什么用户的用户? [英] How do I find out what user owns what process programmatically?
本文介绍了我如何找出以编程方式拥有什么用户的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,所以我要尝试的是找出给定进程所属的用户的名称.
Okay, so what I'm attempting to do is find out the name of the user for which a given process belongs to.
Process[] processList = Process.GetProcesses();
foreach (Process p in processList)
{
Console.WriteLine(p.Id);
}
Console.ReadLine();
当前,我可以找到每个进程的进程ID,但不能找到用户.如果我知道进程ID,是否可以告诉拥有该进程的用户是谁?
Currently, I can find out the process ID of each process, but not the user. Is there a way to tell who the user is that owns the process if I know the process ID?
推荐答案
You can use the Win32_Process class from the WMI to get all the info related to a process.
检查此示例
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public static string GetProcessOwner(int PID, out string User)
{
string DummyStr = String.Empty;
User = String.Empty;
string ProcessStr = String.Empty;
try
{
ObjectQuery WMIQuery = new ObjectQuery(string.Format("Select * from Win32_Process Where ProcessID ={0}", PID.ToString()));
ManagementObjectSearcher WMIResult = new ManagementObjectSearcher(WMIQuery);
if (WMIResult.Get().Count == 0) return DummyStr;
foreach (ManagementObject oItem in WMIResult.Get())
{
string[] List = new String[2];
oItem.InvokeMethod("GetOwner", (object[])List);
ProcessStr = (string)oItem["Name"];
User = List[0];
if (User == null) User = String.Empty;
string[] StrSID = new String[1];
oItem.InvokeMethod("GetOwnerSid", (object[])StrSID);
DummyStr = StrSID[0];
return DummyStr;
}
}
catch
{
return DummyStr;
}
return DummyStr;
}
static void Main(string[] args)
{
string User;
Process[] processList = Process.GetProcesses();
foreach (Process p in processList)
{
GetProcessOwner(p.Id,out User);
Console.WriteLine(p.Id.ToString()+' '+User);
}
Console.ReadLine();
}
}
}
更新
您还可以将所有者和pid存储在词典中,以提高性能.
UPDATE
also you can store in a Dictionary the owners and the pid, for improve the performance.
using System;
using System.Diagnostics;
using System.Management;
using System.Text;
using System.Collections.Generic;
namespace ConsoleApplication2
{
class Program
{
public static Dictionary<int, string> GetAllProcessOwners()
{
Dictionary<int, string> d = new Dictionary<int, string>();
//string DummyStr = String.Empty;
string User = String.Empty;
string ProcessStr = String.Empty;
try
{
ObjectQuery WMIQuery = new ObjectQuery("Select * from Win32_Process");
ManagementObjectSearcher WMIResult = new ManagementObjectSearcher(WMIQuery);
if (WMIResult.Get().Count == 0) return d;
foreach (ManagementObject oItem in WMIResult.Get())
{
string[] List = new String[2];
oItem.InvokeMethod("GetOwner", (object[])List);
ProcessStr = (string)oItem["Name"];
User = List[0];
if (User == null) User = String.Empty;
//string[] StrSID = new String[1];
//oItem.InvokeMethod("GetOwnerSid", (object[])StrSID);
//DummyStr = StrSID[0];
d.Add(Convert.ToInt32(oItem["ProcessId"]), User);
}
}
catch (Exception E)
{
Console.WriteLine(E.Message);
return d;
}
return d;
}
static void Main(string[] args)
{
Dictionary<int, string> List = GetAllProcessOwners();
Process[] processList = Process.GetProcesses();
foreach (Process p in processList)
{
if (List.ContainsKey(p.Id))
{
Console.WriteLine(p.Id.ToString() + ' ' + List[p.Id]);
}
}
Console.ReadLine();
}
}
}
这篇关于我如何找出以编程方式拥有什么用户的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文