1

How to get the current logged in user?

I already tried

Environment.CurrentUser 

and

WindowsIdentity.GetCurrent().

Both of them return the user that is running the programm.

Thanks for your help.

Kind Regards, Sandro

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sandrome
  • 13
  • 4

1 Answers1

1

Add the directive System.Management.

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];

Alternatively check out this accepted answer.

Community
  • 1
  • 1
C4d
  • 3,183
  • 4
  • 29
  • 50