41

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
22
  • 1
    Environment.UserName works fine here. Commented Mar 5, 2014 at 3:15
  • 1
    @Codemunkeee Why is the answer there "not what you wanted"? Does it produce incorrect output? Commented Mar 5, 2014 at 3:20
  • 1
    Of course you had to hide the only important bit of code under the dialog. Commented Mar 5, 2014 at 3:26
  • 1
    If you application is running under another user, you will get that other user name. Commented Mar 5, 2014 at 3:27
  • 2
    possible duplicate of How do I get the AD Display Name of the currently logged in user Commented Mar 5, 2014 at 5:12

3 Answers 3

76

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.

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

4 Comments

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 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.
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
For the PowerShell peeps that ended up here from search: Add-Type -Assembly 'System.DirectoryServices.AccountManagement'; $userDisplayName = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current.DisplayName
-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);
    }
}

6 Comments

@Codemunkeee When describing a problem, saying "it's not working" is useless. In order to receive help you have to tell us what is going wrong. Do you see an exception? If so, what is the exception? Is the result unexpected? How so?
You can refer to my question above. As you can see it returns a different output, I'll edit the code again to list all the things that I did..
@Codemunkeee can you please try the above code and paste what console output you are getting
still the same bro :(
okay.. wait.. I'm still editing the code and all the things that I've used
|
-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.

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.