Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C#
Article

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.9K   5.8K   29   21
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.

Image 1

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)


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

Comments and Discussions

 
Questionhow to start the service Pin
Himanshu_Gautam22-May-17 21:21
Himanshu_Gautam22-May-17 21:21 
GeneralInstallMe() does not work for me Pin
Member 125050605-May-16 6:04
Member 125050605-May-16 6:04 
GeneralRe: InstallMe() does not work for me Pin
Member 141123878-Jan-19 19:54
Member 141123878-Jan-19 19:54 
QuestionThank you!? Pin
omidt2023-Dec-13 20:37
omidt2023-Dec-13 20:37 
QuestionInstalling .NET Windows Services (the easiest way) Pin
radesi22-Aug-13 7:04
radesi22-Aug-13 7:04 
QuestionAwesome Pin
DominionZA17-Jul-13 5:14
DominionZA17-Jul-13 5:14 
GeneralMy vote of 5 Pin
vaerge28-Feb-13 14:05
vaerge28-Feb-13 14:05 
GeneralMy vote of 5 Pin
Abkarino27-Apr-11 22:54
Abkarino27-Apr-11 22:54 
GeneralBeware of uninstall issue Pin
Kamran Behzad12-May-10 16:45
Kamran Behzad12-May-10 16:45 
QuestionHow can I define startup parameters for a windows service while installing it? Pin
Member 25855272-Mar-09 7:07
Member 25855272-Mar-09 7:07 
Hi Sudheer Reddy Battula!
Thank you for the nice article. I got a problem with installing a windows service. How can I define startup parameters for a windows service while installing it?

Here is the detailed story which I think that it would be helpful for you help Smile | :)
I wrote and installed my custom windows service. I didn’t use the right mouse click to create a service installer instead I derived it from the “Installer” class and wrote it myself. Now I need to use my service sometimes as a windows application. In the main Main() method of my application I made a check for parameters. If there is a parameter called “-ui” it works as a Windows application. If there is not a parameter it checks if it’s already installed into the windows services or not. If not installed then it installs itself and runs. The service/windows application runs well. Now I just want it to run it without a parameter as a Windows application and with a parameter as a service (the total opposite as I am using it right now). So I changed my Main() method for the parameters. With a double click on the exe it runs as windows application. For starting the service now the application is expecting a parameter called “-service” from the command line. The service must also be notified that it can take parameters. By double clicking in the “services” window we can see “Path to executable" (which is the “ImagePath” key in the registry). I modified the value of the “ImagePath” key by hand and the service worked well. Now I want to skip the editing the registry by hand part and automate it. First I tried add the parameter into the assembly location.

this.Context.Parameters["assemblypath"]= Assembly.GetEntryAssembly().Location + " -service";

this modification truly changed the see “Path to executable" of my service in the services window and and “ImagePath” in the registry like this:

“myPath\MyService.exe –service”

but the funny thing was that my service stoped working after the modification. The reason that it was working when I modified the registry by hand was that the ”-service” parameter wasn’t in the double quotes. Instead it was like this.
“myPath\MyService.exe’ –service
I checked several web sites to figure out how to define startup parameters into a windows service and I always came to a code which modified the registry key “ImagePath”. like this

using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
{
if (system == null)
return;

using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
{
if (currentControlSet == null)
return;

using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
{
if (services == null)
return;

using (RegistryKey service = services.OpenSubKey("MyService"))
{
string imagePath = service.GetValue("ImagePath") as string;
service.SetValue("ImagePath", imagePath + " -service");
service.Close();
}
services.Close();
}
currentControlSet.Close();
}
system.Close();
}

But now I got permission issues to write to the registry. How can I solve this problem. Because I can normally write to the registry otherwise it would not write to it in the first place.
In the end thank you in advance…
Sincerely
Mehmet Rasim Inceoglu
AnswerRe: How can I define startup parameters for a windows service while installing it? Pin
yipmonster23-Apr-10 9:07
yipmonster23-Apr-10 9:07 
GeneralFigured it out... Pin
Steve Messer21-Jun-08 20:11
Steve Messer21-Jun-08 20:11 
GeneralRe: Figured it out... Pin
Sudheer Reddy Battula21-Jun-08 20:52
Sudheer Reddy Battula21-Jun-08 20:52 
GeneralRe: Figured it out... Pin
Steve Messer22-Jun-08 5:25
Steve Messer22-Jun-08 5:25 
GeneralNot the solution you are looking for but... [modified] Pin
Steve Messer20-Jun-08 11:24
Steve Messer20-Jun-08 11:24 
GeneralPlease include links to referenced articles Pin
Zoltan Balazs19-Jun-08 11:44
Zoltan Balazs19-Jun-08 11:44 
GeneralRe: Please include links to referenced articles Pin
Sudheer Reddy Battula19-Jun-08 12:07
Sudheer Reddy Battula19-Jun-08 12:07 
GeneralSoooo... Pin
PIEBALDconsult19-Jun-08 9:29
mvePIEBALDconsult19-Jun-08 9:29 
GeneralRe: Soooo... Pin
Sudheer Reddy Battula19-Jun-08 11:41
Sudheer Reddy Battula19-Jun-08 11:41 
GeneralRe: Soooo... Pin
PIEBALDconsult19-Jun-08 15:06
mvePIEBALDconsult19-Jun-08 15:06 
GeneralRe: Soooo... Pin
Sudheer Reddy Battula20-Jun-08 7:30
Sudheer Reddy Battula20-Jun-08 7:30 

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.