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 Service 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.
In the below example, you can see how to list out all the 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 the windows service list:

Check whether the service is exists or not:

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 –

Recommended:   Install and Uninstall Windows Service using PowerShell
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