I've written a Windows Service that exposes a WCF service to a GUI installed on the same machine. When I run the GUI, if I can't connect to the service, I need to know if it's because the service app hasn't been installed yet, or if it's because the service is not running. If the former, I'll want to install it (as described here); if the latter, I'll want to start it up.
Question is: how do you detect if the service is installed, and then having detected that it's installed, how do you start it up?
using (var sc = ServiceController. GetServices(). FirstOrDefault(s => s. ServiceName == "myservice")) - I think this is a better approach.
A Windows service is a long-running application that can be started automatically when your system is started. You can pause your service and resume or even restart it if need be. Once you have created a Windows service, you can install it in your system using the InstallUtil.exe command line utility.
Windows has always used the Services panel as a way to manage the services that are running on your computer. You can easily get there at any point by simply hitting WIN + R on your keyboard to open the Run dialog, and typing in services. msc.
Use:
// add a reference to System.ServiceProcess.dll using System.ServiceProcess; // ... ServiceController ctl = ServiceController.GetServices() .FirstOrDefault(s => s.ServiceName == "myservice"); if(ctl==null) Console.WriteLine("Not installed"); else Console.WriteLine(ctl.Status);
You could use the following as well..
using System.ServiceProcess; ... var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == serviceName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With