Kieren Johnstone
Kieren Johnstone

Reputation: 42003

Environment.UserName gives different results for same user (casing): alternative/transformation?

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

Answers (2)

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

It seems this is simply not possible.

Upvotes: 0

Ian Boyd
Ian Boyd

Reputation: 256731

Get the name of their profile folder:

SHGetKnownFolderPath(FOLDERID_Profile, 0, 0, &s);

e.g. C:\Users\KJohnstone

Upvotes: 1

Related Questions