Windows Service Management






4.40/5 (12 votes)
Apr 5, 2002
2 min read

182527

4377
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)
Figure 1
2. Add a Reference on your application, click menu Project ->
Add Reference and choose System.ServiceProcess.dll
Figure 2
3. Build your GUI like figure 3
Windows forms:
-
Label
-
Button
-
Panel
-
ListView
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 |
Figure 4
4. Add this line of code on the top of file form1.cs
using System.ServiceProcess;
and add a variable on Form1 class
public ServiceController[] services;
5. This is the code when the "View All" button is clicked:
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
Figure 5
Figure 6
Here's the code:
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
ListChange();
8. The code line when start button is clicked:
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:
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