Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Get Current Window User Name

Author: DevASP
Download Source Code : 363_GetCurrentWindowUserName.zip

This article is about how to get the current window’s login user name using window API’s in vb.net. In some situations in your application you want to welcome the user of your application.

  • Steps you will do. 
  • Start visual studio and create a new windows application. 
  • Set the following properties of the form.
Text =’Current Window Login User Name’

 

Drop a label control on the form and set its following properties.

 

Text = ’Its easy to get the current window login user name.’

 

Drop a button control on your form and set its following properties.

 

Name = ‘btGet’

Text = ‘Current User Name’

 

Now open your code window and declare the following function which will be used to access window API’s.

 

Declare Function GetUserName Lib "advapi32.dll" Alias _

"GetUserNameA" (ByVal lpBuffer As String, _

ByRef nSize As Integer) As Integer

Write the following function to get the current login user name.

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

 

Write following code in the click event of button control to show a message box.

 

MessageBox.Show("The current user name is : " + GetUserName(), "Current Window User..", MessageBoxButtons.OK, MessageBoxIcon.Information)

Article Comments
awww very easy tutorial! Your Great! Thanks!!

Posted on 8/5/2010 7:36:26 AM by Priya

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to create a Data Table at Run Time

Disclaimer - Privacy
© 2002-2017 DevASP.net