C# 如何获取进程所有者的用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/300449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get the UserName of the owner of a process?
提问by Andrew Moore
I'm trying to get a list of processes currently owned by the current user (Environment.UserName
). Unfortunately, the Process
class doesn't have any way of getting the UserName of the user owning a process.
我正在尝试获取当前用户 ( Environment.UserName
)当前拥有的进程列表。不幸的是,Process
该类没有任何方法可以获取拥有进程的用户的 UserName。
How do you get the UserName of the user which is the owner of a process using the Process
class so I can compare it to Environment.UserName
?
您如何获取用户的用户名,该用户是使用Process
该类的进程的所有者,以便我可以将其与 进行比较Environment.UserName
?
If your solution requires a pinvoke
, please provide a code example.
如果您的解决方案需要pinvoke
,请提供代码示例。
采纳答案by Martin Hollingsworth
The CodeProject article How To Get Process Owner ID and Current User SIDby Warlibdescribes how to do this using both WMI and using the Win32 API via PInvoke.
在CodeProject上的文章如何让流程所有者的ID和当前用户的SID由Warlib介绍了如何同时使用WMI,并使用通过的PInvoke在Win32 API来做到这一点。
The WMI code is much simpler but is slower to execute. Your question doesn't indicate which would be more appropriate for your scenario.
WMI 代码要简单得多,但执行速度较慢。您的问题并未表明哪种更适合您的情况。
回答by palehorse
You might look at using System.Management (WMI). With that you can query the Win32_Process tree.
您可能会考虑使用 System.Management (WMI)。有了它,您可以查询 Win32_Process 树。
回答by Oskar
回答by Andrew Moore
Thanks, your answers put me on the proper path. For those who needs a code sample:
谢谢,您的回答使我走上了正确的道路。对于那些需要代码示例的人:
public class App
{
public static void Main(string[] Args)
{
Management.ManagementObjectSearcher Processes = new Management.ManagementObjectSearcher("SELECT * FROM Win32_Process");
foreach (Management.ManagementObject Process in Processes.Get()) {
if (Process["ExecutablePath"] != null) {
string ExecutablePath = Process["ExecutablePath"].ToString();
string[] OwnerInfo = new string[2];
Process.InvokeMethod("GetOwner", (object[]) OwnerInfo);
Console.WriteLine(string.Format("{0}: {1}", IO.Path.GetFileName(ExecutablePath), OwnerInfo[0]));
}
}
Console.ReadLine();
}
}
回答by Wolf5
You will have a hard time getting the username without being an administrator on the computer.
如果不是计算机管理员,您将很难获得用户名。
None of the methods with WMI, through the OpenProcess or using the WTSEnumerateProcesses will give you the username unless you are an administrator. Trying to enable SeDebugPrivilege etc does not work either. I have still to see a code that works without being the admin.
除非您是管理员,否则 WMI 的任何方法、通过 OpenProcess 或使用 WTSEnumerateProcesses 都不会为您提供用户名。尝试启用 SeDebugPrivilege 等也不起作用。我仍然需要看到一个无需管理员即可工作的代码。
If anyone know how to get this WITHOUT being an admin on the machine it is being run, please write how to do it, as I have not found out how to enable that level of access to a service user.
如果有人知道如何在没有管理员身份的情况下获得它正在运行的机器,请写下如何做到这一点,因为我还没有找到如何为服务用户启用该级别的访问权限。
回答by sean.net
Props to Andrew Moore for his answer, I'm merely formatting it because it didn't compile in C# 3.5.
支持 Andrew Moore 的回答,我只是对其进行格式化,因为它没有在 C# 3.5 中编译。
private string GetUserName(string procName)
{
string query = "SELECT * FROM Win32_Process WHERE Name = \'" + procName + "\'";
var procs = new System.Management.ManagementObjectSearcher(query);
foreach (System.Management.ManagementObject p in procs.Get())
{
var path = p["ExecutablePath"];
if (path != null)
{
string executablePath = path.ToString();
string[] ownerInfo = new string[2];
p.InvokeMethod("GetOwner", (object[])ownerInfo);
return ownerInfo[0];
}
}
return null;
}
回答by Words Like Jared
You'll need to add a reference to System.Management.dll for this to work.
您需要添加对 System.Management.dll 的引用才能使其工作。
Here's what I ended up using. It works in .NET 3.5:
这是我最终使用的。它适用于 .NET 3.5:
using System.Linq;
using System.Management;
class Program
{
/// <summary>
/// Adapted from https://www.codeproject.com/Articles/14828/How-To-Get-Process-Owner-ID-and-Current-User-SID
/// </summary>
public static void GetProcessOwnerByProcessId(int processId, out string user, out string domain)
{
user = "???";
domain = "???";
var sq = new ObjectQuery("Select * from Win32_Process Where ProcessID = '" + processId + "'");
var searcher = new ManagementObjectSearcher(sq);
if (searcher.Get().Count != 1)
{
return;
}
var process = searcher.Get().Cast<ManagementObject>().First();
var ownerInfo = new string[2];
process.InvokeMethod("GetOwner", ownerInfo);
if (user != null)
{
user = ownerInfo[0];
}
if (domain != null)
{
domain = ownerInfo[1];
}
}
public static void Main()
{
var processId = System.Diagnostics.Process.GetCurrentProcess().Id;
string user;
string domain;
GetProcessOwnerByProcessId(processId, out user, out domain);
System.Console.WriteLine(domain + "\" + user);
}
}