Kation
Kation

Reputation: 61

How to run a program as a service

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?

Upvotes: 0

Views: 325

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

One approach is to use command line argument to specify that (and obviously check in Main). I.e. when configuring service set command line argument to "-asservice" and than in Main check if this argument is passed in. I.e.

if(args.Any(a => a == "-asservice"))
{ 
     // running as service...
}

Upvotes: 1

Related Questions