Results 1 to 5 of 5

Thread: How to Get Current Username in VB.NET

  1. #1
    Join Date
    Oct 2008
    Posts
    54

    How to Get Current Username in VB.NET

    This code demonstrates how to retrieve the username of the current windows user in VB.NET

    Code:
    Declare Function GetUserName Lib "advapi32.dll" Alias _
    
           "GetUserNameA" (ByVal lpBuffer As String, _
    
           ByRef nSize As Integer) As Integer
    
     
    
       Public Function GetUserName() As String
    
           Dim iReturn As Integer
    
           Dim userName As String
    
           userName = New String(CChar(" "), 50)
    
           iReturn = GetUserName(userName, 50)
    
           GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
    
       End Function

  2. #2
    Join Date
    May 2009
    Posts
    511

    Re: How to Get Current Username in VB.NET

    You possibly will more easily obtain the username through just typing:
    CONSOLE.WRITELINE (ENVIRONMENT.USERNAME)
    One line of code obtains the current username. No controls, not anything else required, just the reference toward the System Class. Put the line of code within Sub Main () and that is all you necessitate. I hope this will help you to solve your query thank you.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Get Current Username in VB.NET

    Yes, the Environment.Username usually does an internal call to the Win32 API function GetUserName and as per the documents submitted here - http://msdn.microsoft.com/library/de...etusername.asp, it brings back the user associated with the current thread. So mostly it does the job but if you call it from a Windows Service then it wont work for you.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Get Current Username in VB.NET

    I will simply add the below code to get username in VB.Net

    Code:
    System.Environment.UserName
    It is very simple you know but if you want to use anymore then you can also use the below code:

    Code:
    System.Security.Principal.WindowsIdentity.GetCurre nt().Name
    Or else, you can also copy the below code:

    Code:
    Imports System.Security
    Imports System.Security.Principal
    
    AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal)
    Dim myID As WindowsIdentity = WindowsIdentity.GetCurrent()
    Msgbox("The Current User is : " & myID.Name)

  5. #5
    Join Date
    Feb 2014
    Posts
    3

    Re: How to Get Current Username in VB.NET

    This is the code for getting current username

    public static string UserName { get; }

    try this code

    for more technology updates visit infotuck.com

Similar Threads

  1. Change the username in Windows 7
    By TheGreatOne in forum Tips & Tweaks
    Replies: 8
    Last Post: 05-12-2011, 11:38 AM
  2. Every process is without username
    By Erakna in forum Operating Systems
    Replies: 6
    Last Post: 05-08-2011, 03:10 PM
  3. Need help for getting the DSL username and password
    By Road-Runner in forum Networking & Security
    Replies: 4
    Last Post: 03-12-2010, 05:18 PM
  4. Get dominoGroup from username by using asp.net
    By superkelvin in forum Software Development
    Replies: 4
    Last Post: 24-07-2009, 05:48 PM
  5. SID not resolving to Username
    By Nadeem in forum Active Directory
    Replies: 5
    Last Post: 05-01-2009, 06:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,547,441.67314 seconds with 17 queries