40

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
DdW
  • 890
  • 1
  • 18
  • 30
Codemunkeee
  • 1,585
  • 5
  • 17
  • 29

3 Answers3

69

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.

Note: Doesn't work when machine is unplugged from the network or domain server is not reachable.

J Santosh
  • 3,808
  • 2
  • 22
  • 43
Tim
  • 14,999
  • 1
  • 45
  • 68
  • 9
    I used this approach, but in the real world I'm seeing the getter for UserPrincipal.Current throwing a PrincipalServerDownException. I believe the case is when the current user is logged in with a domain account, but the directory server can't be contacted, such as when a laptop has been unplugged from the network. Use this method with caution, it doesn't seem to be reliable! – Roger Sanders Mar 19 '15 at 21:42
  • 1
    Roger Sanders is right, use this variable with caution. In my case there was a long delay on the network so this method hangs for a while. – Lumo Oct 26 '17 at 11:41
  • 1
    `Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. ` – AgentFire Jul 17 '19 at 09:00
-3

Try this

http://msdn.microsoft.com/en-us/library/sfs49sw0(v=vs.110).aspx

using System.IO;
using System;
using System.Security.Principal;
class Program
{
    static void Main()
    {
        Console.WriteLine(WindowsIdentity.GetCurrent().Name);
    }
}
Dexters
  • 2,419
  • 6
  • 37
  • 57
-4

This is contained within the System.DirectoryServices namespace so you need to add this in the using section.

Then you can use System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName which returns the Display Name. This is typically shown in the Start Menu.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ash.sh
  • 1