简体   繁体   中英

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.

Get the name of their profile folder:

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

eg C:\Users\KJohnstone

It seems this is simply not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM