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);
    }
}

windows_services.gif

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();
}

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments