Current User's FullName...

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

Well, when you pull down the start menu in XP it Usually prints your name right at the top of the menu. I believe it's the same name that is in the "Full Name" field of the Add User dialog. However, of course, on my current work computer there is no "local" account, as I'm logged onto a full network, etc, and so I'm going through a domain of some sort. (Don't know too much about that stuff, and frankly don't really care.)

However, I did look up and found some information about requesting the full user name. (not just your login user name). For example:

Thomas Thumb logs in as : tthumb
Start menu it says: Thumb, Thomas
Email Account: 99thutho

So in effect to do certain things in my application it is rather imperative to retrieve the current users "Full Name"

This is how I am currently doing it:
VB.NET:
         Dim DomainUser As String = My.User.Name.Replace("\", "/")
         Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
         _userfull = New NameStruct(ADEntry.Properties("FullName").Value)
Don't both with the NameStruct, it is simply a parser to determine First Name and Last Name.

Now the problem with this is that when My computer is not connected to the network I get an Exception at the line:
VB.NET:
ADEntry.Properties("FullName").Value

Which makes some sense, but also is somewhat amusing, because when I disconnect from the network (and my laptop goes into standby with the lid down so i can take it home), I later open the lid and log back in and everything is the same, so It still has the information of "Thumb, Thomas" on the start bar, even though I'm no longer connected to the current network. So how can i ask this computer, (not some network service) for the current users Full Name.

Thanks
 
One way is to use the WMI class Win32_UserAccount, compare Caption and My.User.Name to get Fullname. Generate a VB.Net class as described in thread http://www.vbdotnetforums.com/articles-interest/13972-wmi-code-creator-1-0-a.html post 5 and add it to project. Sample code:
VB.NET:
For Each user As Win32.UserAccount In Win32.UserAccount.GetInstances
    If user.Caption = My.User.Name Then
        Return user.FullName
    End If
Next
 
Awesome. That is beautiful..

I even took it one step further, and rewrote some of the Generated code, upgrading the Enumerator to be an IEnumerator(of UserAccount), then switched your suggested loop into a condition based collection call, which makes life easier (especially when there are several 100s of accounts on the main domain).
VB.NET:
dim users as UserAccountCollection = users.GetInstances( _
                  Format(My.User.Name.Substring(My.User.Name.LastIndexOf("\") + 1), "Name = '{0}'"))

After that call, users will either contain 1 or 0 entries in its Enumerator.

Thanks so much. (and that WMI Generator is a wonderful boon too, as now I'm going to start looking into other classes.)
I saw Win32_Printer and Win32_UserAccount, where might I find a list of More? Those Could be VERY helpful in the future.

Thanks
 
I also tried the generated GetInstances(condition) method where condition was caption='my.user.name' but I got a "invalid query" exception for unknown reason, thus I posted the loop. I have used the condition overload before so I know it works, but I can't tell why this condition didn't.
I saw Win32_Printer and Win32_UserAccount, where might I find a list of More?
MSDN has the full WMI reference, you can install the full library locally or use it online.
 
Yea, actually, at first I tried the Caption condition too and got the same error, but i thought I was doing it wrong. So Instead, I printed out in the loop, Caption, Name, Fullname to see how it was working and saw the name was the user name minus the domain. in the online help it showed them using the Name property for the query condition with a Win32_Process object setup similar to this one, so I mimicked it and found that the Name property worked, even if the Caption property did not. *shrug*

the amusing little discrepancies, :D

As for the WMI reference, thanks, i'll look into that on MSDN.
 
Type NameStruct is not defined

Getting the error Type NameStruct is not defined? where I have to reference it.

Okay,

Well, when you pull down the start menu in XP it Usually prints your name right at the top of the menu. I believe it's the same name that is in the "Full Name" field of the Add User dialog. However, of course, on my current work computer there is no "local" account, as I'm logged onto a full network, etc, and so I'm going through a domain of some sort. (Don't know too much about that stuff, and frankly don't really care.)

However, I did look up and found some information about requesting the full user name. (not just your login user name). For example:

Thomas Thumb logs in as : tthumb
Start menu it says: Thumb, Thomas
Email Account: 99thutho

So in effect to do certain things in my application it is rather imperative to retrieve the current users "Full Name"

This is how I am currently doing it:
VB.NET:
         Dim DomainUser As String = My.User.Name.Replace("\", "/")
         Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
         _userfull = New NameStruct(ADEntry.Properties("FullName").Value)
Don't both with the NameStruct, it is simply a parser to determine First Name and Last Name.

Now the problem with this is that when My computer is not connected to the network I get an Exception at the line:
VB.NET:
ADEntry.Properties("FullName").Value

Which makes some sense, but also is somewhat amusing, because when I disconnect from the network (and my laptop goes into standby with the lid down so i can take it home), I later open the lid and log back in and everything is the same, so It still has the information of "Thumb, Thomas" on the start bar, even though I'm no longer connected to the current network. So how can i ask this computer, (not some network service) for the current users Full Name.

Thanks
 
Back
Top