Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

windows

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.

like image 852
Kieren Johnstone Avatar asked Nov 14 '22 22:11

Kieren Johnstone


1 Answers

Get the name of their profile folder:

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

e.g. C:\Users\KJohnstone

like image 197
Ian Boyd Avatar answered Dec 22 '22 10:12

Ian Boyd