Monday 27 June 2016

How to create windows service, component and installer in C#

In this post i will describe
How to create windows service in C# which can found in service.msc?
How to install windows service .msi using InstallUtil tool?
How to create installer for windows service?

So, at first to create windows service we should go to
visual studio > Install > Templates > Visual C# > Windows Desktop  > Windows service

In service1.cs we need to write c# code like this

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;
using System.Threading.Tasks;
using System.Timers;

namespace WinSerivices
    public partial class Winservices : ServiceBase
        public Winservices()

        Timer timer = new Timer();
        string needtobeadd = "";

        protected override void OnStart(string[] args)
            EventLog.WriteEntry("Service started");//Entry in event viewer
            writeLog("Windows service started.");//writing custom log
            timer.Interval = 2000;
            timer.Elapsed += new ElapsedEventHandler(timer_tick);
            timer.Enabled = true;

        protected override void OnStop()
            writeLog("Windows service stopped.");

        void timer_tick(object sender, ElapsedEventArgs e)
            writeLog("Windows service running.");

        void writeLog(string text)
            string file = AppDomain.CurrentDomain.BaseDirectory + "log.txt";
            needtobeadd += DateTime.Now.ToString("O")  + "    " + text  + System.Environment.NewLine;
                File.AppendAllText(file, needtobeadd);
                needtobeadd = "";
            }catch(Exception exc){


Go to setup and deployment project

In the service project do the following:

Right click on your solution and add a new project: Add > New Project > Setup and Deployment Projects > Setup Wizard

a. This could vary slightly for different versions of Visual Studio.
b. Visual Studio 2010/12/13 it is located in: Install Templates > Other Project Types > Setup and Deployment > Visual Studio Installer
On the second step select "Create a Setup for a Windows Application."
On the 3rd step, select "Primary output from..."
Click through to Finish.
Next edit your installer to make sure the correct output is included.

Right click on the setup project in your Solution Explorer.
Select View > Custom Actions. (In VS2008 it might be View > Editor > Custom Actions)
Right-click on the Install action in the Custom Actions tree and select 'Add Custom Action...'
In the "Select Item in Project" dialog, select Application Folder and click OK.
Click OK to select "Primary output from..." option. A new node should be created.
Repeat steps 4 - 5 for commit, rollback and un-install actions.
You can edit the installer output name by right clicking the Installer project in your solution and select Properties. Change the 'Output file name:' to whatever you want. By selecting the installer project as well and looking at the properties windows, you can edit the Product Name, Title, Manufacturer, etc...

Next build your installer and it will produce an MSI and a setup.exe. Choose whichever you want to use to deploy your service.

or else
To install your service manually

start up the command prompt (CMD) with administrator rights.
cd c:\windows\\framework\v4.0.30319\
installutil.exe [your windows service path to exe]
Press Enter.

To uninstall your service manually
cd c:\windows\\framework\v4.0.30319\
installutil /u <yourproject>.exe