h1

Installing Windows services programmatically

June 5, 2008

[updated in this post.]

using System;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace SimpleWindowsServiceManager
{
    class Program
    {
        static void Main(string[] args)
        {
            string fullpath = args[0];
            string servicename = Path.GetFileNameWithoutExtension(Path.GetFileName(args[0]));

            #region Test to see if it's a service
            Assembly assembly = null;
            try
            {
                assembly = Assembly.LoadFile(fullpath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Couldn't load assembly: " + ex.Message);
                return;
            }
            AssemblyInstaller installer = new AssemblyInstaller(assembly, null);
            if (installer.Installers.Count == 0)
            {
                Console.WriteLine("Assembly apparently doesn't contain a service");
                return;
            }
            #endregion

            #region Install service
            try
            {
                // For options here take a look at installutil in the framework directory
                Console.WriteLine("Installing " + fullpath);
                ManagedInstallerClass.InstallHelper(new string[] { fullpath });
            }
            catch (InvalidOperationException iex)
            {
                Console.WriteLine("Install not applicable for this assembly:\n" + iex.Message);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            Console.WriteLine("Getting service controller for " + servicename);
            ServiceController controller = new ServiceController(servicename);

            #region Start service
            try
            {
                controller.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            Thread.Sleep(10000);

            #region Stop service
            try
            {
                controller.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            Thread.Sleep(10000);

            #region Uninstall service
            try
            {
                ManagedInstallerClass.InstallHelper(new string[] { "/LogToConsole=false", "/u", args[0] });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion
        }
    }
}

5 comments

  1. i’m trying to pass custom parameters into the installhelper by doing something like

    ManagedInstallerClass.InstallHelper(new string[] { fullpath, “/username=[JEFF]” });

    but it doesn’t seem to be working at all. can you please shed some light on how to do that? thanks.


  2. Hey — yep, I had to do this too; I’ve got some modified code, I’ll see if I can dig it out and post it.


  3. i think i was able to figure it out, the problem seemed to have been that i was placing the variables AFTER the fullpath of my exe.


  4. [...] Barrass’ Weblog « Installing Windows services programmatically Installing Windows Services programatically pt. 2 September 25, 2008 [An update of this [...]


  5. Aye :) For what it’s worth, I put an example of the kind of thing I switched to in this post



Leave a Comment