Codemunkeee Codemunkeee - 6 months ago 46
Vb.net Question

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

Tim Tim
Answer

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.