Display Windows Services in C#

Display Windows Services in C#

.NET Framework 2.0 provides System.ServiceProcess namespace that contains classes developers can use to create and install Windows Services. These classes include ServiceBase, ServiceInstaller and ServiceProcessInstaller. This namespace also contains one class which can display the list of all the Windows Services running on local or remote computer. In this Tutorial I will show you how you can display the list of locally running Windows Services in C#.

You can write following code at Form load even to populate listbox control with all locally running Windows Services. You need to import System.ServiceProcess namespace for the code example below.

private void Form1_Load(object sender, EventArgs e)
{
    ServiceController[] localServices = ServiceController.GetServices();
    foreach (ServiceController service in localServices)
    {
        listBox1.Items.Add(service.DisplayName);
    }
}

Write following code at listbox SelectedIndexChanged Event to get information about currently selected Windows Service.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ServiceController[] localServices = ServiceController.GetServices();
    ServiceController service = localServices[listBox1.SelectedIndex];

    label4.Text =  service.DisplayName;
    label5.Text =  service.ServiceName;
    label6.Text =  service.Status.ToString();
}
READ ALSO:  Introduction to LINQ to SQL

This Post Has One Comment

  1. Avatar
    Sabin Shrestha

    excellent tutorials. thanks for posting such an informative materials.

Leave a Reply