How to list all the dependent windows services using C#

In C#, the System.ServiceProcess namespace of System.ServiceProcess.ServiceController.dll has the ServiceController class which can be used to operate with windows services. In this article learn how to list all the dependent windows services using C#.


List out all the dependent windows services using C#:

Microsoft Windows services are formerly known as NT services. Microsoft windows services allow you to create long-running executable applications that run in their own Windows sessions. These services can also be started, stopped, paused, or restarted programmatically by the external code.
In the below example, you can see how to list out all the dependent windows services through C#.

Note: Always make sure that run the application in Administrator mode in order to start the windows service. Otherwise your application may encounter security exception.

Get all dependent windows service list:

Get all windows services dependent on given windows service name:

Complete source code:

using System;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set the name of choice of your windows service name intended to start.
            string serviceName = "MyService";

            // Read all the windows services.
            ServiceController[] serviceList = GetAllDependentWindowsServiceList(serviceName);
            foreach (ServiceController service in serviceList)
            {
                Console.WriteLine("Service Name: " + service.ServiceName);
                Console.WriteLine("Service Display Name: " + service.DisplayName);
                Console.WriteLine("Service Machine Name: " + service.MachineName);
            }
        }

        /// <summary>
        /// Get the windows dependent services list for the given service name.
        /// </summary>
        private static ServiceController[] GetAllDependentWindowsServiceList(string serviceName)
        {
            // Create the service controller instance for the 'serviceName'.
            ServiceController service = new ServiceController(serviceName);

            //Get the dependent services.
            ServiceController[] serviceList = service.DependentServices;

            return serviceList;
        }

        /// <summary>
        /// Get the windows service which is dependent on the given service name.
        /// </summary>
        private static ServiceController[] GetAllServicesDependentOnServiceName(string serviceName)
        {
            // Create the service controller instance for the 'serviceName'.
            ServiceController service = new ServiceController(serviceName);

            //Get the dependent services.
            ServiceController[] serviceList = service.ServicesDependedOn;

            return serviceList;
        }
    }
}

– Article ends here –

Recommended:   How to log Windows Service custom logs in C#
Share this:

Kiran is a Software engineer by profession and Enthusiastic blogger, love to write articles on computer technology and programming. Reach him at kiran.troubleshootyourself@gmail.com.

We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo
Shopping cart