...Technology Simplified

Wednesday, July 11, 2012

Windows Service -- creation , Installation , Uninstallation

No comments :

In this post we will have a look at how to create windows service in c# and how to install or uninstall and how to start service in detail.
First let us know what a window service is and uses of windows service and then we will see how to create windows service.

What is Windows Service?

Windows Services are applications that run in the background and perform various tasks. The application that does not need any user interface. Windows Services are started automatically when computer is booted. Windows Services are controlled through the Service Control Manager where they can be stopped, paused, and started as needed.

How to Create a Windows Service

Open visual studio (I’m using Visual studio 2010) --> Select File --> New -->Project--> select Windows Service from c#


click ok button to create our project that should like this


In Solution explorer rename Service1.cs file to SampleService.cs.

And then right click on the page to select Properties then change ServiceName to SampleService.


Open SampleService.cs in design view and right click on it to Add Installer files to our application. The main purpose of using Windows Installer is an installation and configuration service provided with Windows. The installer service enables customers to provide better corporate deployment and provides a standard format for component management.


You can now see two controls: ServiceProcessInstaller1 and ServiceInstaller1

Now right click on serviceProcessInstaller1 and select properties in that change Account toLocalSystem


Now right click on ServiceInstaller1 and change properties

StartType to Automatic

DisplayName to ‘SampleService’

After completion of setting all the properties now we need to write the code to run the windows services.
We implement windows service to write entry every minute into a file.
If you observe our project structure that contains Program.cs file that file contains Main() method otherwise write the Main() method like this in Program.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
static class Program
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
new SampleService()
Open SampleService.cs file and add namespaces for writing to file and timerin codebehind of SampleService.cs file

using System.IO;
using System.Timers;
  public partial class SampleService : ServiceBase
Timer timer = new Timer();

public SampleService()

protected override void OnStart(string[] args)
TraceService("start service");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = true;

protected override void OnStop()
timer.Enabled = false;
TraceService("stopping service");

private void OnElapsedTime(object source, ElapsedEventArgs e)
TraceService("Another entry at " + DateTime.Now);

private void TraceService(string content)
FileStream fs = new FileStream(@"C:\SampleService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
In OnStart method the event ElapsedEventHandler is used to run the windows service for every one minute
Then build the application and install the service.
To install windows service in your follow these steps
Start --> All Programs --> Microsoft Visual Studio 2010 --> Visual Studio Tools --> Open Visual Studio Command Prompt
After open command prompt point to your windowsservice1.exe file in your project

Installutil windowsservice1.exe (Give your windows service exe file name) and now press enter button.


After successful installation of service, to view it:
Start --> Control Panel --> Open Control Panel --> Select Administrative Tools --> Computer Management --> Services and Applications --> Services --> Open services

Right click on sampleservice and start the service.
Note: If we want to uninstall the installed windows service go to VS 2010 command prompt and point to your service same as for installation and type statement installutil /u windowsservicesample.exe
Once the service is started, and you will be able to see entries in the log file we defined in the code.

Now open the log file in your folder that Output of the file like this


No comments :

Post a Comment