Library code snippets
Get Current Username in VB.NET
By Walter Steed, published on 09 Feb 2004
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
Related articles
Related discussion
-
Why choose c# when there is Java
by Thaj06 (0 replies)
-
Change color while typing in rich text box
by Akhtar Hussain (0 replies)
-
Third party components to enhance User Interface of Windows based application
by Shaila14041981 (0 replies)
-
VB.NET DataGridView Sort
by bradhen (0 replies)
-
About Comparison in Asp.Net
by S_Rahul (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
Environment.Username returns the name of the user running the current process.
If the current process is running under the System/LocalService/NetworkService accounts, it would just give you one of these rather than the logged-in user.
Environment.Username returns the name of the user running the current process.
If the current process is running under the System/LocalService/NetworkService accounts, it would just give you one of these rather than the logged-in user.
Imports System.Security
Imports System.Security.Principal.WindowsIdentity
After Importing the above
Add this to a click event of the button.
MsgBox(GetCurrent.Name)
Environ("username") does not work in non Domain OS's (95/98). Alternate code is required.
You could more easily get the username by just typing:
CONSOLE.WRITELINE(ENVIRONMENT.USERNAME)
One line of code gets the current username. No controls, nothing else needed, just the reference to the System Class. Place the line of code in Sub Main() and that is all you need.
This thread is for discussions of Get Current Username in VB.NET.