Click here to Skip to main content
13,905,154 members
Click here to Skip to main content
Add your own
alternative version


205 bookmarked
Posted 22 Nov 2007
Licenced CPOL

Windows Services Can Install Themselves

, 22 Nov 2007
Rate this:
Please Sign up or sign in to vote.
Never use the InstallUtil.exe utility that ships with the .NET SDK again.


Using the InstallUtil.exe utility that ships with the .NET SDK can be a real pain. It's rarely in the PATH so you probably have to hunt down the utility when you are working on QA and production servers as I do. Installing a Windows Service should be easier. In this short article, I'll show you a way to make your Windows Services install themselves without needing InstallUtil.exe at all.


Let's assume that your service project has a service installer, a service process installer and a class derived from System.Configuration.Install.Installer already. If not, check out Mahmoud Nasr's excellent article on Windows Service development, then come back here.

Key Information

Thanks to Reflector for .NET by Lutz Roeder, it's easy to discover how the InstallUtil.exe utility does its job. After some setup, the InstallUtil.exe tool jumps to a method called InstallHelper in the ManagedInstallerClass in the System.Configuration.Install namespace. And what's really interesting is that the command line arguments passed to InstallUtil.exe as an array of strings are then passed directly to this helper method.

Well, this made me think, "If all InstallUtil.exe does is call the ManagedInstallerClass' InstallHelper method, why can't my service executable do the same thing to install itself on command?" The little class below makes it simple to do just that.

Using the Code

Create a new CS file in your service executable project containing the following code. You may also need to add a reference to the System.Configuration.Install.dll from the Global Assembly Cache if you don't already have one.

using System.Reflection;
using System.Configuration.Install;

    public static class SelfInstaller
        private static readonly string _exePath =
        public static bool InstallMe()
                    new string[] { _exePath } );
                return false;
            return true;

        public static bool UninstallMe()
                    new string[] { "/u", _exePath } );
                return false;
            return true;

Now you need to come up with some sort of convention for knowing when to invoke the installer. Below is just an example of how you might handle this in your Main() method, the entry point to your service. I like the convention of using -i or -install parameters to install the service and -u or -uninstall to uninstall it. I also like to use -c or -console to mean starting the application in a console rather than as a service. However, that's a topic for a different article.


namespace MyService.WinHost
    static class Program
        public static void Main( string[] args )
            if (args != null && args.Length == 1 && args[0].Length > 1
                && (args[0][0] == '-' || args[0][0] == '/'))
                switch (args[0].Substring( 1 ).ToLower())
                    case "install":
                    case "i":
                    case "uninstall":
                    case "u":
                    case "console":
                    case "c":

Now, assuming my executable is named MyWinSvcHost.exe, I can invoke the installer by running:

C:\> MyWinSvcHost.exe -install

Or to uninstall my service, I would use this:

C:\> MyWinSvcHost.exe -uninstall

Other Ideas

This little bit of code called the SelfInstaller is full of possibilities. You could pass parameters to the InstallMe method to pass on to the ServiceProcessInstaller in your program, for example. Perhaps the domain name, user name and password used to start your service could be passed all the way from your Main() method to the ServiceProcessInstaller. Cool, right? I thought you would like that.


  • 22 Nov, 2007 - Initial publication


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


About the Author

W. Kevin Hazzard
Web Developer
United States United States
After 16 years as an ardent C++ aficionado, Kevin switched to C# in 2001. Recently, Kevin's been dabbling in dynamically typed languages. Kevin is the Software Architect for, the #1 source for hourly and part-time employment on the web.

Kevin loves welding, riding motorcycles and spending time with his family. Kevin has also been an adjunct professor teaching software engineering topics at a college in his hometown of Richmond, Virginia since 2000. Check out Kevin's technical blog at for more goodies.

You may also be interested in...


Comments and Discussions

GeneralJust like Delphi does it. Pin
stensones22-Nov-07 23:33
memberstensones22-Nov-07 23:33 
GeneralExcellent article!!! Pin
ndinges22-Nov-07 22:52
memberndinges22-Nov-07 22:52 
GeneralGreat Article Pin
Cool Dirty Boy22-Nov-07 21:48
memberCool Dirty Boy22-Nov-07 21:48 
GeneralGreat article Pin
NikoTanghe22-Nov-07 21:43
memberNikoTanghe22-Nov-07 21:43 
GeneralExcellent! [modified] Pin
TheCardinal22-Nov-07 21:34
memberTheCardinal22-Nov-07 21:34 
GeneralGreat !!! Pin
Gismow22-Nov-07 20:57
memberGismow22-Nov-07 20:57 
Generalgood article Pin
margiex22-Nov-07 19:14
membermargiex22-Nov-07 19:14 
General5 Pin
Ri Qen-Sin22-Nov-07 19:37
memberRi Qen-Sin22-Nov-07 19:37 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 22 Nov 2007
Article Copyright 2007 by W. Kevin Hazzard
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid