Detect if code is running as a service

前端 未结 9 1425
谎友^
谎友^ 2020-12-10 16:30

Is there a way for an .NET library to detect whether or not it is being run as a service?

My library can be run either way. But when its run as a service, developer

9条回答
  •  孤街浪徒
    2020-12-10 16:36

    A quick and dirty way is to apply a command line switch to the entry in the registry under HKLM\System\CurrentControlSet\services\MyService\ImagePath and then check for that switch in your main function.

    You then know if you were started by services.exe or not. Yeah it's a hack.

提交回复
热议问题