How to create a simple Windows Service in C# and Install it using Installutil


Back to learning
Created: 05/02/2013

How to create a simple Windows Service in C#  and Install it using Installutil


I will explain just a simple method to deploy your own windows service.

1) First we will create a Windows Service project.




2) Make sure you have the similar basic structure like this.




3) Now very important, we need to add installer controll to our project, for this we will create our custom installer like this: Add simpe c# class and call it like "MyWindowsServiceInstaller.cs"



4) Now to use all the required classes in our project we need to add one reference for "System.Configuration.Install" library and appropriate references in our class.




5) Now we need to add some code to our installer to make it installer it self. Just do it like this:





I puth here the Constructor code:

----------------------------------------
public MyWindowsServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller =
                              new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            serviceProcessInstaller.Username = null;
            serviceProcessInstaller.Password = null;

            //# Service Information
            serviceInstaller.DisplayName = "My New C# Windows Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            //# This must be identical to the WindowsService.ServiceBase name
            //# set in the constructor of WindowsService.cs
            serviceInstaller.ServiceName = "My Windows Service";

            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }

----------------------------------------

6) Now build your service application and go to Debug folder to see your app.exe



7) Thats all now we will install this service using InstallUtil and CMD

Got to run and open CMD AS ADMINISTRATOR!!


Make sure that you have this file: C:\Windows\Microsoft.NET\Framework\v2.0.50727\
InstallUtil.exe

Write in CMD: "cd\" then press Enter

Copy your InstallUtil url and past it on CMD like this:

cd C:\Windows\Microsoft.NET\Framework\v2.0.50727\ and press enter



And now we need to install our Service that is located in our Debug folder using InstallUtil.
So just type the follow line: InstallUtil /i "C:\Users\Mak\Documents\Visual Studio 2008\Projects\ MyServiceApp\MyServiceApp\bin\Debug\MyServiceApp.exe"

You will obtain the following window:



That meen that your service was installed and you can find it here:



-----------------------------------------------------------------------------------------------------------------------------------
To uninstall this service just use "/u" instead of "/i" like:

InstallUtil /u "C:\Users\Mak\Documents\Visual Studio 2008\Projects\MyServiceApp\ MyServiceApp\bin\Debug\MyServiceApp.exe"

-----------------------------------------------------------------------------------------------------------------------------------

Hope this helps somebody!
If you found any incoherence in this article, just denunce it
above this site!

To create the installer class i used code that i found in google, you can modify it to get the proper results.

Thank you for your visit!