Search code examples
windowsvb.netactive-directorywindowsdomainaccount

How to get UserName from Computer?


I'm searching to get back the String on the red block. The red block is the string

I already tried the following code:

Environment.UserName
My.User.Name
SystemInformation.UserName
System.Security.Principal.WindowsIdentity.GetCurrent().Name
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Environment.GetFolderPath(Environment.GetEnvironmentVariable("%USERNAME%"))

But this code don't work, because the computer of my company uses a domain, and the String that I need is different as the user. The user is composed only by the name, and the string is composed by the name and the first name of the employees.


Solution

  • I finally found what I need here

    We have to import that :

    “System.DirectoryServices.AccountManagement”
    “System.DirectoryServices”

    And write this two lines to get back the red block.

    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current