Click here to Skip to main content
Click here to Skip to main content

Installing .NET Windows Services (the easiest way)

, 19 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Installing .NET Windows services without using InstallUtil.exe


I will demonstrate the easiest way of installing a Windows service. Using the InstallUtil.exe had been my primary mechanism of installing a Windows service until I came across Windows Services Can Install Themselves by W. Kevin Hazzard. That article has demonstrated that a service can be installed/uninstalled/launched from the command line alone. I, on the other hand, have come up with a way to install/uninstall the Windows service by double clicking the service executable.

The traditional command line approach is as follows:

C:\> InstallUtil MyWinService.exe

Kevin Hazzard’s command line approach is as follows:

C:\> MyWinSvcHost.exe -install

My approach: Just double click on the Service EXE.

Using the Code

The first double click on the service executable will install the service and the second double click on the service executable will uninstall the service. Whenever we run a Windows service, the main method gets executed as this serves as the entry point for the service. The list of existing Windows services can be fetched using the GetServices method of the ServiceController class. Loop through the array of services to determine if our service is already installed.

If the service does not exist on the machine, we will install it by executing the InstallMe method of the SelfInstaller class, followed by a message box notification of the same.



If the service does not exist on the machine, we will uninstall it by executing the UninstallMe method of the SelfInstaller class, followed by a message box notification of the same.



As Kevin said in his article, this style of installing the service without using the InstallUtil.exe provides us with many options of installing and invoking the service. Happy coding, my dear fellow s/w brothers. You can also find this and my other posts at Cavemansblog.


Version 2.0: Code can be downloaded from the link at the top of this article (See version 2).

  1. Added SMESSER's code to get rid of the annoying alert box (cannot start service from the command line or a debugger) that appears after the service has been installed/uninstalled.
  2. Added code to make this approach more secure (Thanks to PIEBALDconsult's concern) by prompting the user, is he/she would REALLY like to install/uninstall the service.
  3. Moved the WSInstaller class to WSInstaller.cs for cleaner code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sudheer Reddy Battula

United States United States
Sudheer Reddy Battula - Caveman's Blog

Comments and Discussions

QuestionThank you!? Pinmemberomidt2023-Dec-13 21:37 
QuestionInstalling .NET Windows Services (the easiest way) Pinmemberradesi22-Aug-13 8:04 
QuestionAwesome PinmemberMichael Smit17-Jul-13 6:14 
GeneralMy vote of 5 Pinmembervaerge28-Feb-13 15:05 
GeneralMy vote of 5 PinmemberAbkarino27-Apr-11 23:54 
GeneralBeware of uninstall issue PinmemberKamranBehzad12-May-10 17:45 
QuestionHow can I define startup parameters for a windows service while installing it? PinmemberMember 25855272-Mar-09 8:07 
AnswerRe: How can I define startup parameters for a windows service while installing it? Pinmemberyipmonster23-Apr-10 10:07 
GeneralFigured it out... Pinmembersmesser21-Jun-08 21:11 
GeneralRe: Figured it out... PinmemberSudheer Reddy Battula21-Jun-08 21:52 
GeneralRe: Figured it out... Pinmembersmesser22-Jun-08 6:25 
GeneralNot the solution you are looking for but... [modified] Pinmembersmesser20-Jun-08 12:24 
I had already implemented the command-line version of the self installing application.

As another reader posted a double-click to uninstall is not something that I would
feel comfortable with so I used a combination of the command-line and your double-click

Installation is only by double click and only installs if the service is not installed and returns.
Un-Installation is only via the command-line.

   MessageBox.Show("Successfully installed the " + SERVICE_NAME, "Status", 
   MessageBoxButtons.OK, MessageBoxIcon.Information);

This way you have an very easy to install service that is also easy to uninstall but not so
easy that it you are unaware of it happening Smile | :)

modified on Friday, June 20, 2008 5:51 PM

GeneralPlease include links to referenced articles PinmemberZoltan Balazs19-Jun-08 12:44 
GeneralRe: Please include links to referenced articles PinmemberSudheer Reddy Battula19-Jun-08 13:07 
GeneralSoooo... PinmemberPIEBALDconsult19-Jun-08 10:29 
GeneralRe: Soooo... PinmemberSudheer Reddy Battula19-Jun-08 12:41 
GeneralRe: Soooo... PinmemberPIEBALDconsult19-Jun-08 16:06 
GeneralRe: Soooo... PinmemberSudheer Reddy Battula20-Jun-08 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 19 Jun 2008
Article Copyright 2008 by Sudheer Reddy Battula
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid