I'm trying to start a service in a custom action for my WiX Setupfor a project C#.
At first I check if service is started :
[CustomAction]
public static ActionResult StopService(Session session)
{
ServiceController MyService = null;
try
{
MyService = new ServiceController("MyService");
if (MyService != null) &&(MyService.Status != ServiceControllerStatus.Stopped))
{
MyService.Stop();
MyService.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 2, 0));
}
session.Log("Stop service");
}
catch (Exception ex)
{
session.Log(ex.ToString());
}
}
But there is an Exception when I try to get the status (MyService.Status
) :
System.InvalidOperationException: Impossible d'ouvrir le service MyService sur l'ordinateur '.'. ---> System.ComponentModel.Win32Exception: Le service spécifié n’existe pas en tant que service installé --- Fin de la trace de la pile d'exception interne --- à System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) à System.ServiceProcess.ServiceController.GenerateStatus() à System.ServiceProcess.ServiceController.get_Status() à CustomAction.CustomActions.StopService(Session session)
Translation : "Unable to open service MyService on this computer -> Specified service doesn't not exist as installed service.
How can I check if a service is installed ? (I checked, MyService is not null)