开发者

Launch Condition - Check if a service is running

开发者 https://www.devze.com 2022-12-27 13:09 出处:网络
In VS2008, how can I check whether 开发者_StackOverflow中文版a windows service is running int he launch condition? For details of how to Check that a service is running in Launch Conditions, see this

In VS2008, how can I check whether 开发者_StackOverflow中文版a windows service is running int he launch condition?


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);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消