Click here to Skip to main content
13,047,567 members (61,021 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 19 Jun 2008

Installing .NET Windows Services (the easiest way)

, 19 Jun 2008
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

You may also be interested in...


Comments and Discussions

AnswerRe: How can I define startup parameters for a windows service while installing it? Pin
yipmonster23-Apr-10 9:07
memberyipmonster23-Apr-10 9:07 
GeneralFigured it out... Pin
smesser21-Jun-08 20:11
membersmesser21-Jun-08 20:11 
GeneralRe: Figured it out... Pin
Sudheer Reddy Battula21-Jun-08 20:52
memberSudheer Reddy Battula21-Jun-08 20:52 
GeneralRe: Figured it out... Pin
smesser22-Jun-08 5:25
membersmesser22-Jun-08 5:25 
While you are in the clean-up mode I generally put the
SelfInstaller class in it's own file as well.
GeneralNot the solution you are looking for but... [modified] Pin
smesser20-Jun-08 11:24
membersmesser20-Jun-08 11:24 
GeneralPlease include links to referenced articles Pin
Zoltan Balazs19-Jun-08 11:44
memberZoltan Balazs19-Jun-08 11:44 
GeneralRe: Please include links to referenced articles Pin
Sudheer Reddy Battula19-Jun-08 12:07
memberSudheer Reddy Battula19-Jun-08 12:07 
GeneralSoooo... Pin
PIEBALDconsult19-Jun-08 9:29
memberPIEBALDconsult19-Jun-08 9:29 
GeneralRe: Soooo... Pin
Sudheer Reddy Battula19-Jun-08 11:41
memberSudheer Reddy Battula19-Jun-08 11:41 
GeneralRe: Soooo... Pin
PIEBALDconsult19-Jun-08 15:06
memberPIEBALDconsult19-Jun-08 15:06 

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

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

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