Windows Forms: How to check Windows Service Status in C#

This post show you How to check Windows Service Status in C#

Sometimes you may need to check the status of windows service, you can do it with following simple way.

Creating a new Windows Forms Application, then design a simple UI as shown below. You can also use console application to run the code below.

c# windows service status

Right-clicking on your Reference, then add System.ServiceProcess to your project.

Adding a click event handler to the check button allows you to check the windows service status.

private void btnCheck_Click(object sender, EventArgs e)
{
    var service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == txtServiceName.Text);
    if (service == null)
        lblStatus.Text = $"Service {txtServiceName.Text} not found!";
    else
        lblStatus.Text = $"Service {txtServiceName.Text} is {service.Status}";
}

and don't forget to add

using System.ServiceProcess;

to your form.

Related