0

Install .Net windows services without using installutil.exe

Posted by Chris Straw on November 2, 2013 in c#, vb.net, Windows Service |

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);
            }

        }
    }

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2011-2019 Chris Straw All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.