1

I have a program that can be installed and started as service, or can be started from SYSTEM account using the 'at' command. I need to find a way to know if the executable was started as service or from SYSTEM account, so I could know if I need to call ServiceBase.Run or perform other work at the executable entry point (Main function).

Environment.UserInteractive is false in both situations.

WindowsIdentity.GetCurrent().IsSystem is true in both situations.

I'd like not to pass any command line arguments to solve this.

Entry point's ReflectedType.BaseType.FullName doesn't work either.

user2102508
  • 1,009
  • 11
  • 23

1 Answers1

0

You could try looking at the parent of your process.

public static bool IsRunningAsService() 
{
    return Process.GetCurrentProcess().Parent().ProcessName == "services";
}
Richard
  • 29,854
  • 11
  • 77
  • 120