如何在C#中确定进程的所有者?我正在寻找名为“MyApp.exe”的进程,我想确保获得特定用户拥有的进程。我使用以下代码获取进程列表:Process[] processes = Process.GetProcessesByName("MyApp");这给了我一个进程列表,但是在Process类中似乎没有办法确定谁拥有该进程?有关如何做到这一点的任何想法?
3 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
这是非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
- 3 回答
- 0 关注
- 583 浏览
添加回答
举报
0/150
提交
取消