User.Name Property

Gets the name of the current user.

Namespace:  Microsoft.VisualBasic.ApplicationServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public string Name { get; }

Property Value

Type: System.String
String. The name of the current user.

You can use the My.User object to get information about the current user.

The principal that authenticates the user also controls the format of the user name. By default, an application uses Windows authentication, and the user name has the format DOMAIN\USERNAME. A custom implementation of the principal does not necessarily use the same format.

NoteNote

The My.User.Name property returns an empty string when run under Windows 95 and Windows 98 because those operating systems do not recognize user names.

NoteNote

The exact behavior of the My.User object depends on the type of the application and on the operating system on which the application runs. For more information, see the User class overview.

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

This example checks if the application is using Windows or custom authentication, and uses that information to parse the My.User.Name property.

Function GetUserName() As String 
    If TypeOf My.User.CurrentPrincipal Is 
      Security.Principal.WindowsPrincipal Then 
        ' The application is using Windows authentication. 
        ' The name format is DOMAIN\USERNAME. 
        Dim parts() As String = Split(My.User.Name, "\")
        Dim username As String = parts(1)
        Return username
    Else 
        ' The application is using custom authentication. 
        Return My.User.Name
    End If 
End Function

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft