My program can run as console program or Windows service. However, when program runs as a service, it runs Main(string[] args)
too.
And my Main()
method have some code that can not use in windows service.
How can I determine if my program is running as a service?
One approach is to use command line argument to specify that (and obviously check in Main
). Ie when configuring service set command line argument to "-asservice" and than in Main
check if this argument is passed in. Ie
if(args.Any(a => a == "-asservice"))
{
// running as service...
}
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.