C# 如何在 .net 中获取进程的用户名或所有者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/566835/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:27:23  来源:igfitidea点击:

How to get the user name or owner of a process in .net

c#.netprocess

提问by Luke Foust

How can I find the owner of a given process in C#? The class System.Diagnostics.Process doesn't seem to have any properties or methods that will get me this information. I figure it must be available because it is shown in the Windows Task Manager under the "User Name" column.

如何在 C# 中找到给定进程的所有者?System.Diagnostics.Process 类似乎没有任何属性或方法可以让我获得这些信息。我认为它必须可用,因为它显示在“用户名”列下的 Windows 任务管理器中。

My specific scenario involves finding the instance of a process (such as taskhost.exe) which is running as "Local Service". I know how to find all the instances of taskhost using

我的特定场景涉及查找作为“本地服务”运行的进程实例(例如 taskhost.exe)。我知道如何使用

Process.GetProcessesByName("taskhost")

So now I just need to know how to identify the one which is running as local service.

所以现在我只需要知道如何识别作为本地服务运行的那个。

回答by casperOne

You can use the Handle property on the process and pass it to GetSecurityInfo through the P/Invoke layer to get the security information on the process.

您可以使用进程上的 Handle 属性,并通过 P/Invoke 层将其传递给 GetSecurityInfo 以获取有关进程的安全信息。

It's the same as this question:

这和这个问题是一样的:

How do I get the SID / session of an arbitrary process?

如何获取任意进程的 SID/会话?

回答by Eric Petroelje

Might want to try the code at this link

可能想尝试此链接中的代码

First result in google search for "C# get process owner"

谷歌搜索“C# get process owner”的第一个结果

Most likely the task manager uses the Win32 API via C to do this. That process is also outlined in the link above.

很可能任务管理器通过 C 使用 Win32 API 来执行此操作。上面的链接中也概述了该过程。

回答by BACON

Use WMI to retrieve instances of the Win32_Process class, then call the GetOwner methodon each instance to get the domain name and user name of the user under which the process is running. After adding a reference to the System.Managementassembly, the following code should get you started:

使用 WMI 检索Win32_Process 类的实例,然后在每个实例上调用GetOwner 方法以获取运行该进程的用户的域名和用户名。添加对System.Management程序集的引用后,以下代码应该可以帮助您入门:

// The call to InvokeMethod below will fail if the Handle property is not retrieved
string[] propertiesToSelect = new[] { "Handle", "ProcessId" };
SelectQuery processQuery = new SelectQuery("Win32_Process", "Name = 'taskhost.exe'", propertiesToSelect);

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(processQuery))
using (ManagementObjectCollection processes = searcher.Get())
    foreach (ManagementObject process in processes)
    {
        object[] outParameters = new object[2];
        uint result = (uint) process.InvokeMethod("GetOwner", outParameters);

        if (result == 0)
        {
            string user = (string) outParameters[0];
            string domain = (string) outParameters[1];
            uint processId = (uint) process["ProcessId"];

            // Use process data...
        }
        else
        {
            // Handle GetOwner() failure...
        }
    }