Click here to Skip to main content
Licence CPOL
First Posted 19 Jun 2008
Views 27,564
Downloads 667
Bookmarked 18 times

Installing .NET Windows Services (the easiest way)

By | 19 Jun 2008 | Article
Installing .NET Windows services without using InstallUtil.exe

Introduction

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.

Install.JPG

S-Install.JPG

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.

UnInstall.JPG

S-UnInstall.JPG

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.

Update

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.

License

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

Software Developer (Senior)

United States United States

Member

My passions in life are science, family, food, movies and sports. I am pursuing a career as a S/W Professional for the past 8 years. I am currently busy with my job life and do not have much time for doing the things I like. However, I do find time to workout and play cricket during summer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberAbkarino22:54 27 Apr '11  
GeneralBeware of uninstall issue PinmemberKamranBehzad16:45 12 May '10  
QuestionHow can I define startup parameters for a windows service while installing it? PinmemberMember 25855277:07 2 Mar '09  
AnswerRe: How can I define startup parameters for a windows service while installing it? Pinmemberyipmonster9:07 23 Apr '10  
GeneralFigured it out... Pinmembersmesser20:11 21 Jun '08  
GeneralRe: Figured it out... PinmemberSudheer Reddy Battula20:52 21 Jun '08  
GeneralRe: Figured it out... Pinmembersmesser5:25 22 Jun '08  
GeneralNot the solution you are looking for but... [modified] Pinmembersmesser11:24 20 Jun '08  
GeneralPlease include links to referenced articles PinmemberZoltan Balazs11:44 19 Jun '08  
GeneralRe: Please include links to referenced articles PinmemberSudheer Reddy Battula12:07 19 Jun '08  
GeneralSoooo... PinmemberPIEBALDconsult9:29 19 Jun '08  
GeneralRe: Soooo... PinmemberSudheer Reddy Battula11:41 19 Jun '08  
GeneralRe: Soooo... PinmemberPIEBALDconsult15:06 19 Jun '08  
GeneralRe: Soooo... PinmemberSudheer Reddy Battula7:30 20 Jun '08  

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.

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