55

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();
                }
            }
        }
    }
}
pstrjds
  • 16,840
  • 6
  • 52
  • 61

3 Answers3

98

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.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • I was questionable on the performance of your solution, so I ran a test. Your solution is far more performative (even with the code added to dispose of the service controllers) when the service does not exist. In the case where the service does exist this solution is twice as slow. I am not sure what to do now. In my case I am expecting that more often than not the service will exist. In that case my solution wins out, in the more generic case your solution wins out. – pstrjds Oct 20 '10 at 18:54
  • One additional comment. Basically this solution runs in constant time whether the service exists or not. Kudos for a good generic case solution. – pstrjds Oct 20 '10 at 19:15
  • In response to the speed issue, the code I presented in the question is faster when the service does exist (although I agree that it is negligible). I would not normally be calling this in a loop (just did that for performance analysis), otherwise I could cache the list. I think the hit is in iterating the list twice. Once to find the service (although not necessarily a complete iteration of the whole list) and another time to dispose of the objects. In the case where I am implementing I cannot cache the list, I may run the function days apart in which case the service could have been removed. – pstrjds Oct 20 '10 at 19:52
  • Note that FirstOrDefault did not appear until .Net 3.5, so something else is required for earlier frameworks. – Michael Paulukonis Feb 18 '11 at 21:21
  • 3
    @pstrjds - you should look up 'performative' so at least then you can utter it under appropriate or conventional circumstances. – CRice Jul 28 '11 at 06:12
  • 3
    @CRice - you are correct, I had never looked the word up before, I had heard it used when discussing performance, I just looked it up now, wow is it a different meaning. I sure hope that the context here showed what was intended and did not lead to any sort of confusion in understanding the question I was asking. Next time I will be sure to post all my questions at [English StackExchange](http://english.stackexchange.com/) first so that I ensure I have proper grammatical and orthographical content. – pstrjds Jul 28 '11 at 12:52
  • You might want to consider changing the `==` operator with `s.ServiceName.Equals(serviceName, StringComparison.CurrentCultureIgnoreCase)` – ZivS May 10 '15 at 05:42
  • Great answer! Is there any danger in changing var service to ServiceController service? – kayleeFrye_onDeck Oct 27 '15 at 19:21
  • 1
    @kayleeFrye_onDeck No danger at all. They are equivalent. – adrianbanks Oct 27 '15 at 19:23
  • But what if i need to take remote server's .... i am unable to perform that – Rush.2707 Feb 24 '17 at 06:55
  • 2
    Note that getting a list of all services via GetServices requires SC_MANAGER_* permissions, that querying a specific service does not. In some environments, you may not have access to enumerate the service list. – Tim Sparkles Mar 28 '17 at 00:41
19

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));
}
Beetee
  • 475
  • 1
  • 7
  • 18
Mike
  • 1,122
  • 2
  • 13
  • 25
  • 1
    You aren't disposing the `ServiceController`s, surely this is bad practice? – Robula Feb 14 '19 at 15:04
  • ServiceController.GetServices() is a static method, therefore no instance to dispose of. The Any() under the hood calls dispose. https://stackoverflow.com/questions/31931596/do-linq-ienumerable-extensions-call-dispose-on-their-ienumerable – JimmyV Apr 28 '20 at 21:43
  • @JimmyV your link details how / when the iterator will be disposed, not the elements inside it. GetServices returns an IEnumerable of IDisposable objects (the ServiceController objects), and nothing in the above code will dispose these automatically. – Matt Burnell Jul 27 '20 at 03:52
4

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;
    }
Shawn
  • 41
  • 2
  • Nice. Here's my "one-liner": `private static bool TryGetService(string name, out ServiceController service) => (service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == name)) != null;` – Philippe Paré Mar 15 '18 at 12:57