Reputation: 4897
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
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
Reputation: 8231
string userID = WindowsIdentity.GetCurrent().Name
From MSDN: WindowsIdentity.GetCurrent() and WindowsIdentity.Name
Upvotes: 64