Zannjaminderson Zannjaminderson - 2 months ago 7
C# Question

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

Answer
string userID = WindowsIdentity.GetCurrent().Name

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