views:

205

answers:

2

In VS2008, how can I check whether a windows service is running int he launch condition?

+2  A: 

For details of how to Check that a service is running in Launch Conditions, see this thread,

The most reliable custom action would be a C++ Dll call inserted before the LaunchConditions action in both UI and Execute sequences.

There's an example of one here:

http://support.microsoft.com/default.aspx?scid=kb;en-us;253683.

Your custom action code can check for the service running and set a property for the LaunchConditions.

You can use ServiceController.GetServices method to list the services that are running on the local computer.

ServiceController[] scServices;
scServices = ServiceController.GetServices();

// Display the list of services currently running on this computer.

Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
    if (scTemp.Status == ServiceControllerStatus.Running)
    {
        // Write the service name and the display name
        // for each running service.
        Console.WriteLine();
        Console.WriteLine("  Service :        {0}", scTemp.ServiceName);
        Console.WriteLine("    Display name:    {0}", scTemp.DisplayName);
    }
}
Jacob Seleznev
+2  A: 

You can connect to the service as follows:

ServiceController sc = new ServiceController("ServiceName");

You can check if the service is running by checking the "Status" property. Status returns a value (enum) of type ServiceControllerStatus.

Seventh Element