vb.net 使用VB.NET查找当前登录用户的用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24364172/
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
Finding user name of current logged in user using VB.NET
提问by Mario LIPCIK
I'm trying to get the user name of the current user. When I log in as Johnny Smith
and run my application without administrator privileges it will return me the correct user name, Johnny Smith
. But the problem is that when I right click and choose "Run as Administrator", Windows will prompt me with a login screen for the administrator and after login my application returns user name admin
, not the user which is logged in currently.
我正在尝试获取当前用户的用户名。当我以身份登录Johnny Smith
并在没有管理员权限的情况下运行我的应用程序时,它将返回正确的用户名Johnny Smith
. 但问题是,当我右键单击并选择“以管理员身份运行”时,Windows 会提示我一个管理员登录屏幕,登录后我的应用程序返回用户名admin
,而不是当前登录的用户。
I have tried:
我试过了:
strUserLabel.Text = Environment.UserName
Also
还
Dim WSHNetwork = CreateObject("WScript.Network")
Dim strUser = ""
While strUser = ""
strUser = WSHNetwork.Username
End While
strUserLabel.Text = strUser
Both return me the administrator user name when prompted as administrator.
当提示为管理员时,两者都返回管理员用户名。
采纳答案by Mario LIPCIK
I have figured it out. I used this function which will determine which process which the user is using. In my code I defined that look for username of the explorer.exe
process.
我已经想通了。我使用了这个函数来确定用户正在使用哪个进程。在我的代码中,我定义了查找explorer.exe
进程的用户名。
Function GetUserName() As String
Dim selectQuery As Management.SelectQuery = New Management.SelectQuery("Win32_Process")
Dim searcher As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher(selectQuery)
Dim y As System.Management.ManagementObjectCollection
y = searcher.Get
For Each proc As Management.ManagementObject In y
Dim s(1) As String
proc.InvokeMethod("GetOwner", CType(s, Object()))
Dim n As String = proc("Name").ToString()
If n = "explorer.exe" Then
Return s(0)
End If
Next
End Function
Indexof 0will return username
索引为0将返回用户名
Indexof 1will return domain nameof user
索引为1将返回用户的域名
回答by dovid
In the MSDN documentation, I discovered they changed the definition of property Environment.UserName
.
在 MSDN 文档中,我发现他们更改了 property 的定义Environment.UserName
。
Gets the user name of the person who started the current thread.
获取启动当前线程的人的用户名。
Gets the user name of the person who is currently logged on to the Windows operating system
获取当前登录到 Windows 操作系统的人的用户名
回答by Hannington Mambo
I think the accepted answer above is a VERY resource intensive way to find a username. It has nested loops with hundreds of items. In my 8GP RAM PC this takes 2+ seconds!
我认为上面接受的答案是一种非常耗费资源的查找用户名的方法。它有数百个项目的嵌套循环。在我的 8GP RAM PC 中,这需要 2 秒以上!
How about:
怎么样:
- Username:
SystemInformation.Username
, and - DomainName:
Environment.UserDomainName
- 用户名:
SystemInformation.Username
, 和 - 域名:
Environment.UserDomainName
Tested in VS2017
在 VS2017 中测试
回答by Dan
SystemInformation.Username
doesn't work for certain applications. In my case, code is being run as System but explorer.exe is being run as Daniel. SystemInformation.Username
reports System.
SystemInformation.Username
不适用于某些应用程序。就我而言,代码以 System 身份运行,但 explorer.exe 以 Daniel 身份运行。SystemInformation.Username
报告系统。