views:

304

answers:

3

Hello,

How do I install a Windows Service programmatically without using installutil.exe?

Thanks

+1  A: 

I install and uninstall my Windows Service via the command line, e.g., MyWindowsService.exe -install and MyWindowsService.exe -uninstall, to avoid using installutil.exe myself. I've written a set of instructions for how to do this here.

Matt Davis
+5  A: 

You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters:

(Added by 280Z28:) Here is a longer reference explaining everything involved with this method: Windows Services Can Install Themselves (End 280Z28)

/// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            if (System.Environment.UserInteractive)
            {

                if (args.Length > 0)
                {
                    switch (args[0])
                    {
                        case "-install":
                            {
                                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                                break;
                            }
                        case "-uninstall":
                            {
                                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                                break;
                            }
                    }
                }
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new MyService() };
                ServiceBase.Run(ServicesToRun);
            }
        }
Mark Redman
+1  A: 

I use the method from the following CodeProject article, and it works great.

Windows Services Can Install Themselves

Dana Holt