Chris Straw
SHARE:

Install .Net windows services without using installutil.exe

Spread the love

When writing a windows service using c# (or vb.net), you can install the service locally using?installutil.exe. ?You can simply just install the service by using a few lines of c# code within your program’s startup. ?By doing this, you will not be able to use?Client Profile of the framework.

By changing the Program.cs (main entry point for your service) to accept arguments, you can install/uninstall the service via a the .Net?ManagedInstallerClass assembly.

   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {

            if (Environment.UserInteractive)
            {
                string parameter = string.Concat(args);
                switch (parameter)
                {
                    case "/install":
                        ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "/uninstall":
                        ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                }
            }
            else
            {
                ServiceBase[] servicesToRun =
                          {
                              new NotificationService()
                          };
                ServiceBase.Run(servicesToRun);
            }

        }
    }