Monday, September 8, 2008

ServiceController class, LINQ and Extension Methods

I ran across a nice article here on how to determine if a Windows Service is installed. I thought this would be better as an extension method. Previously, you would need to catch the InvalidOperationException and ignore it if you tried to use the ServiceController class to get information on a service that is not installed.

public static class ServiceControllerExtensions

{

    public static bool IsInstalled(this ServiceController controller, string serviceName)

    {

        return (from sc in ServiceController.GetServices()

                   where sc.ServiceName.EqualsIgnoreCase(serviceName)

                   select sc).Count() > 0;

    }

}

No comments:

Post a Comment