Reputation: 42003
The .NET call Environment.UserName
for the most part (>99.9% of the time) gives usernames in the same casing - for example Awebb
. Occasionally though I'm seeing AWebb
. Sometimes it seems that it relates the username as the user entered it when logging on, but other times this is confirmed as not the case.
I think this is bad design and a username should be a username, correctly-cased, no matter what: if the user decides to enter aWEbb
then that doesn't mean the OS should start telling apps that that's their username.
That aside, what's the best/other way to obtain the current user's username - ideally without any domain - using the same security permissions required for Environment.UserName
? I know I could normalise everything to lowercase/uppercase but I'm really after obtaining the username correctly capitalised/formatted.
Upvotes: 5
Views: 2966
Reputation: 256731
Get the name of their profile folder:
SHGetKnownFolderPath(FOLDERID_Profile, 0, 0, &s);
e.g. C:\Users\KJohnstone
Upvotes: 1