How do I get the current username in .NET using C#?
In the box, type cmd and press Enter. The command prompt window will appear. Type whoami and press Enter. Your current user name will be displayed.
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
If you are in a network of users, then the username will be different:
Environment.UserName - Will Display format : 'Username'
rather than
System.Security.Principal.WindowsIdentity.GetCurrent().Name - Will Display format : 'NetworkName\Username'
Choose the format you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With