Windows service is a long-running executable that performs specific functions and it is designed not to require user intervention. Windows services can be configured to start when the operating system is booted and run in the background as long as Windows is running, or they can be started manually when required.
[ READ ALSO :- Create Microsoft Word Document by using C# ]
Creating a Windows Service
This example shows step-by-step how to create a windows service in Visual Studio 2010.
1. Start Microsoft Visual Studio 2010. Click File > New > Project. Following Window will appear.
Select the "Visual C#" node under "Project Types". Under Templates, select "Windows Service" and name the project "SampleWindowsService". Click OK Button.
2. When you press OK the following window will appear.
By default, the wizard adds “Service1.cs”; rename it to the name you desire your service to be. Here I’ve named it to “SampleWinService.cs”. By default two overridden method is declared “OnStart ()” and “OnStop ()”.
3. Now for performing your task switch to Code View to write code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace SampelWindowsService
{
public partial class SampleWindowsService : ServiceBase
{
StreamWriter streamWriter;
public SampleWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
streamWriter = new StreamWriter(new FileStream("C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine("Starting Sample Windows Service at " +DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
}
protected override void OnStop()
{
streamWriter = new StreamWriter(new FileStream("C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine("Stopping Sample Windows Service at " + DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
}
}
}
4. Now once the code for windows service is finished. Add a ProjectInstaller to your project. To add project installer right click on the design view of the file and select “Add Installer”.
After adding Installer a new file “ProjectInstaller.cs” will be added to your project. When the file is created two components (ServiceProcessInstaller, ServiceInstaller) are already added to the file.
5. Now select ServiceProcessInstaller1 and change its “Account” property in property explorer to “Local System”.
6. Now select ServiceInstaller1 and change its “Start Type” property to “Automatic” and “Service Name” property to the desired name you want to be displayed. Here I’ve named it to SampleWindowsService.
Now build the project.
Once the project is built successfully we need to install the service.
Installing Windows Service
To install the service open “Visual Studio Command Prompt” as an Administrator.
Now to install the service use the “Installutil” command with the file name of the service created after building your project, situated in “..\bin\debug” folder of the project, with its path.
Example
InstallUtill SampleWinService.exe
This will install your Windows Service on your computer.
To check the service, run the compmgmt.msc command in Run Prompt. Computer Management Window will open. Select the “Services” node under the “Services and Applications”.
You can find your service here with your Service Name.
The Service will start automatically when the System will boot.
You can see the SampleWindowsServiceLogger.txt file in your Computers C:\ drive. This file will contain information about when the service has started or stopped.
Uninstalling Windows Service
In order to uninstall the Windows service, specify the following at the command prompt:
Installutil /u SampleWinService.exe
Leave Comment