Recipe 2.16. Getting the User s Name

Recipe 2.16. Getting the User's Name


You need to obtain the name of the current Windows user.


Use the My.User.Name property to get the domain and login ID of the current user.


The My.User.Name property returns a string containing the current user ID and related domain name in the format "domain/user." If the user is part of a workgroup instead of a domain, the domain portion may be replaced by the local machine name. Applications written using ASP.NET do not have access to the same type of user information as desktop applications because Web Forms programs run in the context of a special web-application user.

If your application uses an authentication system other than the default Windows security scheme, My.User.Name may return information about the current user in a different format.

If you don't like the merged "domain/user" format, you can get the individual components from other areas within the .NET object hierarchy. These three properties will probably get you what you need:

  • System.Environment.MachineName

  • System.Environment.UserDomainName

  • System.Environment.UserName

If you are interested in identifying the registered owner of the local workstation, you can find that information in the system portion of the registry. The key is:


The RegisteredOrganization and RegisteredOwner values within that key supply the values that you often see when installing new software on your system.

See Also

Recipe 2.15 provides additional resources for gathering system-and user-specific details from .NET.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: