Zann Anderson
Zann Anderson

Reputation: 4897

Determine from within code which user my process is running as

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));
}

Upvotes: 35

Views: 37810

Answers (3)

Viktar
Viktar

Reputation: 129

string UserID = Environment.UserName;

Upvotes: 4

Dan J
Dan J

Reputation: 16708

There are a variety of ways, depending on what type of application you're running.

Here's an article on using the Thread Principal to check user data / authorization.

This thread contains a couple of other approaches, though not much elaboration.

Upvotes: 2

Mike Atlas
Mike Atlas

Reputation: 8231

string userID = WindowsIdentity.GetCurrent().Name

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

Upvotes: 64

Related Questions