Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to grab the current users UserName

Status
Not open for further replies.

sicohne

Technical User
Jun 30, 2003
51
GB
Does anybody know how to find out the current users full text Username? To give an example of what I'm looking for, a user logs onto a win2k PC with user ID 1234. Pressing Ctrl+Alt+Del brings up a box saying Logon Information: Joe Bloggs is logged on as 1234. I am after the Joe Bloggs bit. I believe the info is stored on the server but can't seem to grab it.
 
Environment.Username just returns the 1234 bit. It seems to me that it has something to do with Netapi - NetUserGetInfo, but my api knowledge is almost non existent and I would have no idea how to use it.
 
I use this code to retrieve that information:

Imports System.Management

Module User_Account_Info
Dim Scope As Management.ManagementScope
Dim Options As Management.ConnectionOptions
Dim qry As ObjectQuery

Public Function GetFullName(Optional ByVal strUserID As String = "", Optional ByVal strName As String = "") As String

Try
Scope = New ManagementScope("\\" & Environ("ComputerName") & "\root\cimv2", Options)
Scope.Connect()

qry = New ObjectQuery
If strUserID <> "" Then
qry.QueryString = "Select * From Win32_UserAccount Where Domain='Alliance' And Name='" & strUserID & "'"
Else
qry.QueryString = "Select * From Win32_UserAccount Where Domain='Alliance' And FullName Like '*" & strName & "*'"
End If

Dim Searcher As New ManagementObjectSearcher(Scope, qry)

Dim Item As ManagementBaseObject
For Each Item In Searcher.Get()
Return Item("FullName")
Next Item
Catch
Return ""
End Try

End Function

End Module

Hope this helps out.

Shane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top