Vb.net 获取正在运行的进程的用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23279586/
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
Vb.net get username that is running process
提问by Piratica
I'm making a program that looks for processes and can see which user is using them. I've got the scanning code, but not the username code. The username has to be a string. For example: I have 2 people running some processes and the processes will show in the listview. The first column is for processes and the second is for the username. I want it to be like:
我正在制作一个程序来查找进程并可以查看哪个用户正在使用它们。我有扫描码,但没有用户名代码。用户名必须是字符串。例如:我有 2 个人在运行一些进程,这些进程将显示在列表视图中。第一列用于进程,第二列用于用户名。我希望它是这样的:
(process here) (username here)
(此处处理)(此处为用户名)
(process here) (username here)....
(此处处理)(此处为用户名)....
You get the point I think and there's much more processes than that running on the computer. The question is: How do you get the username for someone using the process?
你明白我的意思,而且有比在计算机上运行的进程多得多的进程。问题是:您如何获得使用该流程的人的用户名?
Edit: The code. This is my code.
编辑:代码。这是我的代码。
For Each pr As Process In Process.GetProcesses
Dim lvi As ListViewItem = ListView1.Items.Add(CStr(pr.ProcessName))
'Now I need something to put for the username
Next
This is the other one that is most common.
这是最常见的另一种。
Public Function GetProcessOwner(processId As Integer) As String
Dim query As String = "Select * From Win32_Process Where ProcessID = " + processId
Dim searcher As New ManagementObjectSearcher(query)
Dim processList As ManagementObjectCollection = searcher.[Get]()
For Each obj As ManagementObject In processList
Dim argList As String() = New String() {String.Empty, String.Empty}
Dim returnVal As Integer = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
If returnVal = 0 Then
' argList(0) == User
' argList(1) == DOMAIN
Return argList(0)
End If
Next
Return "NO OWNER"
End Function
回答by Piratica
This code is taken from here, where you can find more information if you need it. Basically, on a console app this will print the process name and the user to the screen:
此代码取自此处,如果需要,您可以在此处找到更多信息。基本上,在控制台应用程序上,这会将进程名称和用户打印到屏幕上:
Public Shared Sub Main()
Dim selectQuery As SelectQuery = New SelectQuery("Win32_Process")
Dim searcher As ManagementObjectSearcher = New
ManagementObjectSearcher(selectQuery)
For Each proc As ManagementObject In searcher.Get
Console.WriteLine(proc("Name").ToString)
Dim s(1) As String
proc.InvokeMethod("GetOwner", CType(s, Object()))
Console.WriteLine(("User: " & (s(1) + ("\" + s(0)))))
Next
Console.ReadLine()
End Sub
This could be implemented as a function, like:
这可以作为一个函数来实现,例如:
Public Function GetUserName(ByVal ProcessName As String)
Dim selectQuery As SelectQuery = New SelectQuery("Win32_Process")
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(selectQuery)
Dim y As System.Management.ManagementObjectCollection
y = searcher.Get
For Each proc As ManagementObject In y
Dim s(1) As String
proc.InvokeMethod("GetOwner", CType(s, Object()))
Dim n As String = proc("Name").ToString()
If n = ProcessName & ".exe" Then
Return ("User: " & s(1) & "\" & s(0))
End If
Next
End Function
Just for reference, proc.InvokeMethod("GetOwner", CType(s, Object()))will return an array like this:
仅供参考,proc.InvokeMethod("GetOwner", CType(s, Object()))将返回一个这样的数组:
- Index 0: Owner/user name
- Index 1: Domain
- 索引0:所有者/用户名
- 索引1:域
And in our case, will store it in s(1).
Hope this helps :)
在我们的例子中,将它存储在s(1).
希望这可以帮助 :)
Notes:
笔记:
If the function returns something like User: \\then the process is probably a special windows process. To see which processes will act like this (for windows users):
如果该函数返回类似的内容,User: \\则该进程可能是一个特殊的 Windows 进程。要查看哪些进程会像这样(对于 Windows 用户):
- Right clickon the task bar
- Select Start Task Manager
- In the Processestab, in the User Namecolumn, some processes will have a blank cell instead of a user name.
- 右键单击任务栏
- 选择启动任务管理器
- 在进程选项卡,在用户名栏中,某些进程将有一个空白单元格,而不是一个用户名。
Edit:
编辑:
The VB.NET function has been edited and should now work, although I have no idea why the console program still worked. At any rate I've left it alone, if it still works why change it?
VB.NET 函数已经过编辑,现在应该可以工作了,虽然我不知道为什么控制台程序仍然可以工作。无论如何,我已经不理会它了,如果它仍然有效,为什么要改变它?

