Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

like image 401
Mario LIPCIK Avatar asked Jun 23 '14 10:06

Mario LIPCIK


1 Answers

In the MSDN documentation, I discovered they changed the definition of property Environment.UserName.

Before .NET 3

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

Starting from version 3

Gets the user name of the person who is currently logged on to the Windows operating system

like image 106
dovid Avatar answered Oct 04 '22 09:10

dovid