Tech Blog

Coding

Check if a Windows Service is up through C#

By Vidhathri Avadhany1 min read
← Back to blog

We have a class called ServiceController in C#. This class helps us to query the windows service. For using this class, we have to use the following imports.

using System;
using System.ServiceProcess;
using System.Linq;

Below are a few functions related to windows services.

Function 1 : Checks if a Windows Service exists

public static bool ServiceExists(string ServiceName)
{
    return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(ServiceName));
}

Function 2 : Checks if a Windows Service is running

public static bool ServiceIsRunning(string ServiceName)
{
    ServiceController sc = new ServiceController
    {
        ServiceName = ServiceName
    };

    if (sc.Status == ServiceControllerStatus.Running)
    {
        return true;
    }
    else
    {
        return false;
    }
}

To avoid any possible exceptions, it is good to check if the service exists before checking if the service is running.

That’s all folks!
Thank you!

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →