Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

Installing .NET Windows Services (the easiest way)

Rate me:
Please Sign up or sign in to vote.
3.17/5 (18 votes)
19 Jun 2008CPOL2 min read 114.7K   5.8K   29  
Installing .NET Windows services without using InstallUtil.exe
	

	<h2>Introduction</h2><p>
		I will demonstrate the easiest way of installing a windows service. Using the InstallUtil.exe has 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 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.

	</p><h2>Using the code</h2><p>
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.

When ever we run a windows service, the main method gets executed as this serves as the entrypoint for the service. The list of of existing windows services can be fetched using the GetServices method of the Service controller class. Loop through the array of services to determine if our service is already installed.

	</p><p>
			 <img src="http://www.codeproject.com/KB/cs/InstallWS/FetchList.JPG" width="535" height="161" alt="" />

	</p>
<p>
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 messagebox notification of the same.
</p>
<p>
<img src="InstallWS/Install.JPG" width="597" height="60" alt="Install.JPG" />
</p>
<p>
<img src="InstallWS/S-Install.JPG" width="270" height="130" alt="S-Install.JPG" />
</p>
<p>
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 messagebox notification of the same.
</p>
<p>
<img src="InstallWS/UnInstall.JPG" width="597" height="60" alt="UnInstall.JPG" />
</p>
<p>
<img src="InstallWS/S-UnInstall.JPG" width="270" height="130" alt="S-UnInstall.JPG" />
</p>
<p>
Well there is one gotcha that I am still working on, that is the message that pops up after the service is installed on uninstalled.
</p>
<p>
<img src="InstallWS/NoProblem.JPG" width="704" height="130" alt="NoProblem.JPG" />
</p>
<p>
This alerts us that a windows service can only be started for execution by the SCM. You can simply hit ok and ignore this message. 
</p>
<p>You can also find this and my other posts at Cavemansblog</p>
<p align="center">Happy coding & Have fun !!!</p>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


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

Comments and Discussions