Click here to Skip to main content
15,886,056 members
Articles / Programming Languages / C#
Article

Windows Service Management

Rate me:
Please Sign up or sign in to vote.
4.40/5 (13 votes)
4 Apr 20022 min read 181.2K   4.4K   79   12
Simple application to manage and control Windows Service

Introduction

When I was reading the MSDN.NET library, I found an interesting topic about windows services. So I tried to build an application by myself about windows service management. This idea comes from the service application that Microsoft has built in the control panel. I hope this application can be become useful.

Building the Application

To build this application I use ServiceController class. This class needs namespace System.ServiceProcess. You can check on your MSDN libary. Here's these steps:

1. New Application C#, specify WinServiceManage as the project name with Window Application (figure 1)

Image 1

Figure 1

2. Add a Reference on your application, click menu Project -> Add Reference and choose System.ServiceProcess.dll

Image 2

Figure 2

3. Build your GUI like figure 3

Windows forms:

  • Label

  • Button

  • Panel

  • ListView

  Image 3

Figure 3

Properties of ListView:

Properties Value
Name ServiceList
FullRowSelect True
GridLines True
Columns look figure 4
MultiSelect False
Sorting Ascending
View Detail

The following shows the properties of the ListView (click button ...). Add 4 column with property:

Column Propertie Propertie value
Name Name ServiceName
  Text Service Name
  Width 126
Description Name Description
  Text Description
  Width 214
Status Name Status
  Text Status
  Width 74

Image 4

Figure 4

4. Add this line of code on the top of file form1.cs    

C#
using System.ServiceProcess;

and add a variable on Form1 class

C#
public <font >ServiceController[] services;</font>

5. This is the code when the "View All" button is clicked:

C#
try
{
 ListViewItem datalist;
 services = ServiceController.GetServices();
 ServiceList.Items.Clear();
 foreach(ServiceController service in services)
 {
     datalist = new System.Windows.Forms.ListViewItem(service.ServiceName.ToString());
     datalist.SubItems.Add(service.DisplayName);
     datalist.SubItems.Add(service.Status.ToString());
     ServiceList.Items.Add(datalist);
 }

}
catch(Exception er)
{
 MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
}

6. Add Method with right mouse clicking on class view. Method name is List

Image 5

Figure 5

Image 6

Figure 6

Here's the code:

C#
public void ListChange()
{
  if(ServiceList.SelectedItems.Count!=0)
  {
      ListViewItem item = ServiceList.FocusedItem;
      if(item!=null)
      {
          if(item.SubItems[2].Text=="Running")
          {
              StartBtn.Enabled = false;
              StopBtn.Enabled = true;
          }
          else
          {
              StartBtn.Enabled = true;
              StopBtn.Enabled = false;
          }
      }
  }
}

7. Method on Event of ListView ie: Click, KeyDown, and KeyUp

C#
ListChange();

8. The code line when start button is clicked:

C#
private void StopBtn_Click(object sender, System.EventArgs e)
{

   try
   {
       if(ServiceList.SelectedItems.Count!=0)
       {
           int n = ServiceList.FocusedItem.Index;
           if(services[n].CanStop)
           {
               services[n].Stop();
               StartBtn.Enabled = true;
               StopBtn.Enabled = false;
               ServiceList.FocusedItem.SubItems[2].Text = "Stopped";
           }
           else
           {
               MessageBox.Show("This service couldn't be stopped","Error",
                               MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }
       else
       {
           MessageBox.Show("Please,choose a service that you want to stop",
                           "Information",MessageBoxButtons.OK,
                           MessageBoxIcon.Information);
       }
   }
   catch(Exception er)
   {
       MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
   }
}

9. The code line when stop button is clicked:

C#
private void StartBtn_Click(object sender, System.EventArgs e)
{
  try
  {
      if(ServiceList.SelectedItems.Count!=0)
      {
          int n = ServiceList.FocusedItem.Index;
          services[n].Start();
          StartBtn.Enabled = false;
          StopBtn.Enabled = true;
          ServiceList.FocusedItem.SubItems[2].Text = "Running";
      }
      else
      {
          MessageBox.Show("Please,choose a service that you want to stop",
                          "Information",MessageBoxButtons.OK,
                          MessageBoxIcon.Information);
      }
  }
  catch(Exception er)
  {
      MessageBox.Show(er.Message,"Error Exception",MessageBoxButtons.OK,
                      MessageBoxIcon.Error);
  }
}

10. Run this application

Reference

MSDN .NET library

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
GeneralThank you Pin
Lakshmipathy1-Mar-09 19:50
Lakshmipathy1-Mar-09 19:50 
QuestionAbout Service controller Pin
Pradeep Sattikar25-Sep-07 19:17
Pradeep Sattikar25-Sep-07 19:17 
Questionto access window service of client from server computer Pin
kk_upadhyay31-Jul-07 2:23
kk_upadhyay31-Jul-07 2:23 
QuestionIssue! Pin
Moin Ahmed6-Nov-06 23:57
Moin Ahmed6-Nov-06 23:57 
Generalwindows service with notifyicons and contextmenu. Help! Pin
chinimimita17-Dec-04 2:16
chinimimita17-Dec-04 2:16 
GeneralRe: windows service with notifyicons and contextmenu. Help! Pin
Xiangyang Liu 刘向阳17-Dec-04 2:47
Xiangyang Liu 刘向阳17-Dec-04 2:47 
GeneralRe: windows service with notifyicons and contextmenu. Help! Pin
chinimimita20-Dec-04 12:16
chinimimita20-Dec-04 12:16 
QuestionHow to add a new service or delete a service? Pin
Jian12345679-Sep-04 5:03
Jian12345679-Sep-04 5:03 
AnswerRe: How to add a new service or delete a service? Pin
Xiangyang Liu 刘向阳17-Dec-04 2:38
Xiangyang Liu 刘向阳17-Dec-04 2:38 
GeneralWow. Pin
Anonymous22-Sep-02 20:14
Anonymous22-Sep-02 20:14 
GeneralRe: Wow. Pin
Tim Hodgson12-Nov-02 3:19
Tim Hodgson12-Nov-02 3:19 
GeneralRe: Wow. Pin
sidownes15-Jan-03 22:50
sidownes15-Jan-03 22:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.