[CodeSnippet] How to get your windows user name - C Sharp

Discussion in 'Programming' started by mourya, Jun 29, 2011.

  1. #1
    Hi, the following c sharp code snippet will help you to get your user name of the current user.


    string myusername = Environment.UserName
    Code (markup):
    There are various other formats that you can get your user name in, try the below and have fun:

    
    string myusername = System.Security.Principal.WindowsIdentity.GetCurrent().Name
    
    string myusername = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\').Last();
    
    string myusername = Environment.GetEnvironmentVariable("USERNAME");
    
    string myusername = System.Windows.Forms.SystemInformation.UserName;
    
    Code (markup):

     
    mourya, Jun 29, 2011 IP