Get Current Username in VB.NET

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

   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

You might also like...


Walter Steed


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”