Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine from within code which user my process is running as

Tags:

c#

process

There's really no pressing reason for me to ask this question other than curiosity - using C#, is there a way to determine from within code which user my process is running as? To illustrate using code:

static void Main(string[] args) {   string userID;   //what goes here to fill in this userID variable?   Console.out.WriteLine(string.Format("This process is running as {0}.", userID)); } 
like image 525
Zann Anderson Avatar asked Nov 23 '10 20:11

Zann Anderson


2 Answers

string userID = WindowsIdentity.GetCurrent().Name 

From MSDN: WindowsIdentity.GetCurrent() and WindowsIdentity.Name

like image 171
Mike Atlas Avatar answered Oct 14 '22 08:10

Mike Atlas


string UserID = Environment.UserName;

like image 44
Viktar Avatar answered Oct 14 '22 06:10

Viktar