我正在寻找名为"MyApp.exe"的进程,我想确保获得特定用户拥有的进程.
我使用以下代码获取进程列表:
Process[] processes = Process.GetProcessesByName("MyApp");
这给了我一个进程列表,但是在Process类中似乎没有办法确定谁拥有该进程?有关如何做到这一点的任何想法?
您可以使用WMI让用户拥有某个进程.要使用WMI,您需要添加System.Management.dll
对项目的引用.
按进程ID:
public string GetProcessOwner(int processId) { string query = "Select * From Win32_Process Where ProcessID = " + processId; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processList = searcher.Get(); foreach (ManagementObject obj in processList) { string[] argList = new string[] { string.Empty, string.Empty }; int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)); if (returnVal == 0) { // return DOMAIN\user return argList[1] + "\\" + argList[0]; } } return "NO OWNER"; }
按进程名称(仅查找第一个进程,相应地调整):
public string GetProcessOwner(string processName) { string query = "Select * from Win32_Process Where Name = \"" + processName + "\""; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processList = searcher.Get(); foreach (ManagementObject obj in processList) { string[] argList = new string[] { string.Empty, string.Empty }; int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)); if (returnVal == 0) { // return DOMAIN\user string owner = argList[1] + "\\" + argList[0]; return owner; } } return "NO OWNER"; }
由于WMI并不总是快速检索信息的方式,因此这是本机P/Invoke的方式:
返回值是null
不成功的.要获取在SYSTEM用户下运行的进程的名称,您需要以管理员身份执行此代码.
private static string GetProcessUser(Process process) { IntPtr processHandle = IntPtr.Zero; try { OpenProcessToken(process.Handle, 8, out processHandle); WindowsIdentity wi = new WindowsIdentity(processHandle); string user = wi.Name; return user.Contains(@"\") ? user.Substring(user.IndexOf(@"\") + 1) : user; } catch { return null; } finally { if (processHandle != IntPtr.Zero) { CloseHandle(processHandle); } } } [DllImport("advapi32.dll", SetLastError = true)] private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CloseHandle(IntPtr hObject);
这是非C#扬声器的VB版本:
Function GetProcessOwner(ProcessName As String) As String Dim query = "Select * from Win32_Process Where Name = """ + ProcessName + """" Dim searcher = New ManagementObjectSearcher(query) Dim processList = searcher.Get() For Each obj As ManagementObject In processList Dim argList As String() = {String.Empty, String.Empty} Dim returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)) If returnVal = 0 Then ' return DOMAIN\user Dim owner = argList(1) + "\\" + argList(0) Return owner End If Next Return "NO OWNER" End Function Function GetProcessOwner(processId As Integer) As String Dim query = "Select * From Win32_Process Where ProcessID = " & processId Dim searcher = New ManagementObjectSearcher(query) Dim processList = searcher.Get() For Each obj As ManagementObject In processList Dim argList As String() = {String.Empty, String.Empty} Dim returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList)) If returnVal = 0 Then ' return DOMAIN\user Return argList(1) + "\\" + argList(0) End If Next Return "NO OWNER" End Function