3

I could probably use Environment.UserName and compare that to 'system' to see if it matches, but that seems hacky and I'm not sure how it would work in a non English environment. Any other suggestions on how to check if the current executable is running as the system account?

Kyle
  • 17,317
  • 32
  • 140
  • 246
  • 2
    http://stackoverflow.com/questions/509292/how-can-i-tell-if-my-process-is-running-as-administrator – NickD Nov 08 '12 at 19:21
  • @Snoopy, those (and even all the links those point to) refer to checking if "running as administrator". Further reading, they are VERY close though. – hometoast Nov 08 '12 at 19:24
  • You working with C# or C++ because `[Environment]::UserName` isn't valid C#. Why do you feel it feels hacky, I see nothing wrong with it, if it does what you want it to do. – Security Hound Nov 08 '12 at 19:30
  • @Ramhound he told you the app **will not run correctly** on non-english language packs. – AgentFire Nov 08 '12 at 19:37
  • You can do the same thing for the `SYSTEM` account - which should have SID `S-1-5-18`: http://support.microsoft.com/kb/243330 – Martin Baulig Nov 08 '12 at 19:40

1 Answers1

8

This should do it:

bool isSystem;
using (var identity = System.Security.Principal.WindowsIdentity.GetCurrent())
{
    isSystem = identity.IsSystem;
}

Never compare the account names. Comparing the user name to "System" would indeed fail on a localized OS.

Ran
  • 5,989
  • 1
  • 24
  • 26