简体   繁体   中英

How to find if Windows Service is started manually or by OS?

I have a Windows service which is started manually.

There is also a restart time in service which makes it to restart itself. For this I exit by giving a >0 number to OS and in the Service -> recovery tab, I have set service to restart on first, second and subsequent failures.

Environment.Exit(1)

But how to find out if service is started manually or by OS?

You can take it from registry :

HKLM\System\CurrentControlSet\Services\YourServiceName

Registry Key: Start (REG_DWORD)

Values: 1 -> System 2 -> Automatic 3 -> Manual 4 -> Disabled

You can also do

new ServiceController("YourServiceName") 

and then do some P/Invoke fun: http://peterkellyonline.blogspot.de/2011/04/configuring-windows-service.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM