12

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.

6
  • ok but I want to remain username of user not of admin. Commented Jun 23, 2014 at 11:03
  • 3
    strUserLabel.Text = Environment.UserName this should work because am using it many forms Commented Jun 23, 2014 at 11:14
  • stackoverflow.com/a/14518508/1271037 Commented Jun 23, 2014 at 11:16
  • 1
    @MarioLIPCIK try strUserLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name and comment what you are getting Commented Jun 23, 2014 at 11:33
  • @MarioLIPCIK strUserLabel.Text =System.Environment.UserName Commented Jun 23, 2014 at 11:41

6 Answers 6

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

i think @Mario LIPCIK should store his user to a string before run as admin for his application so that he can get the actual user from the stored variable
@hector - you can't do that. If you right click and run as admin then the whole process is elevated from the start and is running under a different user
@hector yes my aplication works like that now, but I have several scripts in it which requre admin rights... so when I run my script wil will ask me for every single script to enter password. That is the reason I need my application to run as admin but get corent logged on user
8

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!

How about:

  • Username: SystemInformation.Username, and
  • DomainName: Environment.UserDomainName

Tested in VS2017

Comments

4

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.

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

Index of 0 will return username

Index of 1 will return domain name of user

2 Comments

What is the minimum version of .NET Framework required for this?
Take too long to execute
0

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.

1 Comment

kindly consider adding more information in your asnawer
0

if using Identity Dim UserEmail As String = Context.User.Identity.Name.ToString

Comments

0

Make sure you Imports System then you can use Environment.UserName

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.