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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easier way to get full user name? 2

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
Hey guys,
I just finished this code that returns the current user's full name. I pulls the full name from the active director using the WMI. It works, but one of my coworkers just asked be "Isn't there an easier way to do that?"

Just wondering if anyone knows of a better way, or some built in environment variable that has the full user name.

Code:
Public Function LoggedOnUser() As String
  Dim sReturn as String 
  Dim UserAccount As ManagementObject
  Dim scope As New ManagementScope("\\localhost\root\cimv2")
  scope.Connect()
  Dim FNsearcher As New ManagementObjectSearcher( _
                          scope, _
                          New Management.ObjectQuery( _
                              "SELECT * " & _
                              "FROM Win32_UserAccount " & _
                              "WHERE Name='" & _
                              System.Environment.UserName & "'"))
  For Each UserAccount In FNsearcher.Get()
    sReturn = UserAccount("FullName").ToString
  Next UserAccount
  Return sreturn
End Function

-Rick

----------------------
 
environment.username

is that enough?

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
environment.username returns your username, in most cases that's First Initial Last Name, or something similar. "rway" for example.

What I'm looking for is the full name that is stored with the user properties in the AD. "Way, Rick" for example.

-Rick



----------------------
 
Okay, I found this:

Code:
  Dim DomainUser As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
  Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
  Dim FullName As String = ADEntry.Properties("FullName").Value

which uses directory services to do the same thing.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top