Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Windows User Display Name

How do I get the display name of the user that is logged in? Not the username, but the display name, such as is shown in the screenshot below - and as seen on the start menu in any Windows Vista/7 computer.

enter image description here

I tried a bunch of different suggestions from other questions, but they all show the username, not the display name. You can see the results of these attempts in the above screenshot.

Imports System.Security.Principal Imports System.Threading Imports System.IO Imports System  Public Class Form1      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load         MsgBox("1: " & System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString & vbCrLf & _                "2: " & Environment.UserDomainName & vbCrLf & _                "3: " & WindowsIdentity.GetCurrent().Name & vbCrLf & _                 "4: " & Thread.CurrentPrincipal.Identity.Name & vbCrLf & _                "5: " & Environment.UserName & vbCrLf & _                "6: " & My.User.Name & vbCrLf &                 "7: " & My.Computer.Name)      End Sub  End Class 
like image 627
Codemunkeee Avatar asked Mar 05 '14 03:03

Codemunkeee


People also ask

How do I find my Windows user name?

In the box, type cmd and press Enter. The command prompt window will appear. Type whoami and press Enter. Your current user name will be displayed.

How do I change my display name?

You can do this by clicking the Start button or pressing the Windows key, typing “Control Panel” into the search box in the Start menu, and then clicking on the Control Panel app. Next, click “User accounts.” Click “User accounts” one more time. Now, select “Change your account name” to change your display name.


1 Answers

You should use UserPrincipal.DisplayName:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName 

To do so, you'll need to and add a reference to System.DirectoryServices.AccountManagement.dll from your project.

like image 183
Tim Avatar answered Oct 01 '22 16:10

Tim