Home > DeveloperSection > Articles > Windows Service

Windows Service

Windows Services Windows Services 
2 Comment(s)
 8990  View(s)
Rate this:

Windows Service in .net through C#

Windows services don’t have any user interface, when we want to create any long running code we create windows services which runs in background. It has the capability to start automatically when computer boots.

Example to demonstrate how to create Windows Service and install

To create Windows Service open new project and form new project dialog box select “Windows Service”.

When you press OK the result will look like this

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 are declared “OnStart” and “OnStop”

Now for coding switch to code view.


//here we are creating file so add “System.IO;” namespace to your project.

//overriding OnStart method


protected override void OnStart(string[] args)


//creating file stream to create log file of the event.

            string Filename = @"c:\sample.log";

            FileStream fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);

//writing to file the detail of event occurred.

            sw.WriteLine("Process started at :" + System.DateTime.Now.ToString());





        protected override void OnStop()


            string Filename = @"c:\sample.log";

            FileStream fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);

            sw.WriteLine("Process ended at :" + System.DateTime.Now.ToString());




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 the file and select “Add 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.



Now select ServiceProcessInstaller1 and change its Account property in property explorer to “Local System”.

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 SampleWinService.

Now build the project.

Once the project is built successfully we need to install the service.

Installing the service

To install the service open “Visual Studio Command Prompt” as an Administrator. Now to install the service use “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.


InstallUtill ..\bin\debug\SampleWinService.exe

(‘..’ in the example means the root directory, don’t use ‘..’ write the full path)

This will install the service to your computer.  To check your service write “compmgmt.msc” in Run Prompt, Computer Management Dialog Box will appear. In that select Service under Services and Applications Option.

Here you can see your service running.

To uninstall your service, in the Command Prompt write

Installutil ..\bin\debug\SampleWinService.exe /u

Windows Service in .net through C#

By Awadhendra Tiwari on   6 years ago

Its a good article to start to learn windows service.


Window Service in .net through C#

By Somesh  on   6 years ago

Really , Its a fine article on window service, how use it (easy and simple view)?


Don't want to miss updates? Please click the below button!

Follow MindStick