Check if a service exists on a particular machine without using exception handling

Don't know if there is a better way to do this, so that is the reason for the question. I can check if a service exists on a particular machine with the following code:

bool DoesServiceExist(string serviceName, string machineName)
{
    ServiceController controller = null;
    try
    {
        controller = new ServiceController(serviceName, machineName);
        controller.Status;
        return true;
    }
    catch(InvalidOperationException)
    {
        return false;
    }
    finally
    {
         if (controller != null)
         {
             controller.Dispose();
         }
    }
}

but this seems like an ineffecient solution to me (due to the exception handling). Is there a better way to check if a service exists. Note - I have recently switched to .Net 4.0 so if someone knows of a better solution in 4.0 that would be acceptable.

EDIT: Here is a sample c# console app to test the performance of my example as well as the GetServices code sample. In my testing I found that the GetServices is much more performative in the case where the service does not exist, but is twice as slow when the service does exist:

    static void Main(string[] args)
    {
        string serviceName = string.Empty;
        string machineName = string.Empty;

        var sw = new Stopwatch();
        sw.Reset();
        sw.Start();
        for (int i = 0; i < 1000; i++)
        {
            ServiceExistsException(serviceName, machineName);
        }
        sw.Stop();
        Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());
        sw.Reset();
        sw.Start();
        for (int i = 0; i < 1000; i++)
        {
            ServiceExistsGetList(serviceName, machineName);
        }
        sw.Stop();
        Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());

        Console.WriteLine("Done");
        Console.ReadLine();
    }

    static bool ServiceExistsException(string serviceName, string machineName)
    {
        ServiceController controller = null;
        try
        {
            controller = new ServiceController(serviceName, machineName);
            string name = controller.DisplayName;
            return true;
        }
        catch (InvalidOperationException)
        {
            return false;
        }
        finally
        {
            if (controller != null)
            {
                controller.Dispose();
            }
        }
    }

    static bool ServiceExistsGetList(string serviceName, string machineName)
    {
        ServiceController[] services = null;
        try
        {
            services = ServiceController.GetServices(machineName);
            var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
            return service != null;
        }
        finally
        {
            if (services != null)
            {
                foreach (ServiceController controller in services)
                {
                    controller.Dispose();
                }
            }
        }
    }
}

Solution 1:

You can use the ServiceController.GetServices() method to get all of the services on the machine, then look through them to see if one exists named what you are looking for:

bool DoesServiceExist(string serviceName, string machineName)
{
    ServiceController[] services = ServiceController.GetServices(machineName);
    var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
    return service != null;
}

The FirstOrDefault() extension method (from System.Linq) will return either the first service with the given name, or a null if there is no match.


To address your speed issue:

The difference between the two approaches for a single method call is negligible, regardless of whether the service is found or not. It will only be a problem if you are calling this method thousands of times—in which case get the list of services once and remember it.

Solution 2:

Same approach as adrianbanks but a slight more compact code. If your're using LINQ you can use any statement to return what you want. In addition if you are checking on local computer there's no need to give computer name.

bool DoesServiceExist(string serviceName)
{
   return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
}

Solution 3:

Built on top of Mike's answer. Same concept as Dictionary.TryGetValue.

    /// <summary>
    /// Gets a <see cref="ServiceController"/> given the specified <see cref="pServiceName"/>.
    /// </summary>
    /// <param name="pServiceName">The name of the service.</param>
    /// <param name="pService">The <see cref="ServiceController"/> associated with the name.</param>
    /// <returns>
    /// <see cref="bool.True"/> if the <see cref="ServiceController"/> exists; otherwise <see cref="bool.False"/>.
    /// </returns>
    private static bool TryGetService(string pServiceName, out ServiceController pService)
    {
        pService = ServiceController.GetServices()
            .FirstOrDefault(serviceController => serviceController.ServiceName == pServiceName);

        return pService != null;
    }