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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:32:25  来源:igfitidea点击:

Finding user name of current logged in user using VB.NET

vb.netvisual-studio-2010visual-studio

提问by Mario LIPCIK

I'm trying to get the user name of the current user. When I log in as Johnny Smithand 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.exeprocess.

我已经想通了。我使用了这个函数来确定用户正在使用哪个进程。在我的代码中,我定义了查找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

Before .NET 3

.NET 3 之前

Gets the user name of the person who started the current thread.

获取启动当前线程的人的用户名。

Starting from version 3

从版本 3 开始

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.Usernamedoesn't work for certain applications. In my case, code is being run as System but explorer.exe is being run as Daniel. SystemInformation.Usernamereports System.

SystemInformation.Username不适用于某些应用程序。就我而言,代码以 System 身份运行,但 explorer.exe 以 Daniel 身份运行。SystemInformation.Username报告系统。