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.
You can see how to list all the dependent windows services through C# in the example below.

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

Get all dependent windows service list:

/// 
/// Get the windows dependent services list for the given service name.
/// 
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;
}

Get all windows services dependent on given windows service name:

/// 
/// Get the windows service which is dependent on the given service name.
/// 
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;
}

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 –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   Install and Uninstall Windows Service using InstallUtil.exe
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo