Home All Groups Group Topic Archive Search About

Retrieveing full Username from Enviroment?

Author
7 Apr 2006 2:37 PM
Lars Netzel
Hi

If I use the code:

MsgBox(Environment.UserName)

.... I get the username you log into windows with

But how do I retrieve that Full Name  of that useraccount?

Best Regards/
Lars Netzel

Author
7 Apr 2006 4:04 PM
Vijay
Possible with native API calls i.e InterOp.. I am not sure of the exact
syntax... Check www.pinvoke.net. You will get some idea

VJ

Show quoteHide quote
"Lars Netzel" <uih***@adf.se> wrote in message
news:uuqY7ClWGHA.1192@TK2MSFTNGP04.phx.gbl...
> Hi
>
> If I use the code:
>
> MsgBox(Environment.UserName)
>
> ... I get the username you log into windows with
>
> But how do I retrieve that Full Name  of that useraccount?
>
> Best Regards/
> Lars Netzel
>
Author
10 Apr 2006 2:14 PM
ScottL
If you are in a domain with Active Directory you could query Active
directory to get the information.

Something like:

               'Please note that this code is rough and may need a bit
of tweaking.

                'Hard Coding for simplicity of example
               Dim strcon as string =  "LDAP://mydomain.com"
               Dim strID as string = "MYID"
               Dim strpwd as string = "MYPWD"
               Dim strUserName as string =
user.identity.name.Substring(user.identity.name.IndexOf("\") + 1)


                Dim Root As System.DirectoryServices.DirectoryEntry =
New System.DirectoryServices.DirectoryEntry(strcon, strID, strpwd)
                Dim ds As System.DirectoryServices.DirectorySearcher =
New System.DirectoryServices.DirectorySearcher(Root)
                Dim searchresult As DirectoryServices.SearchResult

                     'set the AD to filter to only the username we are
looking for, then load the ad record
                    ds.Filter = "SAMAccountName=" &  strUserName
                    ds.PropertiesToLoad.Add("cn")
                    For Each searchresult In ds.FindAll()
                        strADReturn =
searchresult.GetDirectoryEntry.Name
                    Next