Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current username in .NET using C#?

Tags:

c#

.net

How do I get the current username in .NET using C#?

like image 748
Yves Avatar asked Aug 06 '09 17:08

Yves


People also ask

How do I find my UserName using CMD?

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.


2 Answers

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
like image 163
juan Avatar answered Oct 07 '22 17:10

juan


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.

like image 40
Israel Margulies Avatar answered Oct 07 '22 17:10

Israel Margulies