gpt4 book ai didi

c# - 如何以编程方式找出哪个用户拥有哪个进程?

转载 作者:行者123 更新时间:2023-11-30 16:34:46 24 4
gpt4 key购买 nike

好的,所以我要做的是找出给定进程所属的用户的名称。

        Process[] processList = Process.GetProcesses();
foreach (Process p in processList)
{
Console.WriteLine(p.Id);

}
Console.ReadLine();

目前我可以查到每个进程的进程ID,但查不到用户。如果我知道进程 ID,有没有办法告诉谁拥有该进程?

最佳答案

您可以使用 Win32_Process从 WMI 类获取与进程相关的所有信息。

检查这个样本

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 存储在字典中,以提高性能。

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();
}
}
}

关于c# - 如何以编程方式找出哪个用户拥有哪个进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2338534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com