How to: List all Windows Service 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 Windows services using C#.


List out all Windows Service using C#:

Microsoft Windows services are formerly known as NT services. Microsoft Windows services enable us 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 out all the windows services through C# in the below example.

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 the windows service list:

/// 
/// Get all the windows services list.
/// 
private static ServiceController[] GetAllWindowsServiceList()
{
    // Get the list of Windows services
    ServiceController[] lstServices = ServiceController.GetServices();

    return lstServices;
}

Check whether the service is exists or not:

/// 
/// Get all the windows services list and find the service name.
/// 
private static bool IsServiceExisted(string serviceName)
{
    bool retVal = false;

    try
    {
        // Get the list of Windows services
        ServiceController[] serviceList = ServiceController.GetServices();

        // Loop through all the windows services.
        foreach (ServiceController service in serviceList)
        {
            // Check whetehr the windows service is found.
            if (string.Compare(service.ServiceName, serviceName) == 0)
            {
                // Set the flag.
                retVal = true;

                break;
            }
        }

        // Return the flag.
        return retVal;
    }
    catch(System.ComponentModel.Win32Exception ex)
    {
        // Handle the exception.
        return false;
    }
}

Complete source code:

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read all the windows services.
            ServiceController[] serviceList = GetAllWindowsServiceList();
            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);
            }


            // Check whether the windows services present or not.
            bool bFlag = IsServiceExisted("MyWindowsServiceName");
            if (bFlag)
            {
                Console.WriteLine("Service found");
            }
            else
            {
                Console.WriteLine("Service not found");
            }
        }

        /// <summary>
        /// Get all the windows services list.
        /// </summary>
        private static ServiceController[] GetAllWindowsServiceList()
        {
            // Get the list of Windows services
            ServiceController[] lstServices = ServiceController.GetServices();

            return lstServices;
        }

        /// <summary>
        /// Get all the windows services list and find the service name.
        /// </summary>
        private static bool IsServiceExisted(string serviceName)
        {
            bool retVal = false;

            try
            {
                // Get the list of Windows services
                ServiceController[] serviceList = ServiceController.GetServices();

                // Loop through all the windows services.
                foreach (ServiceController service in serviceList)
                {
                    // Check whetehr the windows service is found.
                    if (string.Compare(service.ServiceName, serviceName) == 0)
                    {
                        // Set the flag.
                        retVal = true;

                        break;
                    }
                }

                // Return the flag.
                return retVal;
            }
            catch(System.ComponentModel.Win32Exception ex)
            {
                // Handle the exception.
                return false;
            }
        }
    }
}


– Article ends here –

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

Recommended:   Windows Service in C# | Start, Stop, Pause, Resume, Restart code
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo