Example #1
0
        public static void InstallService(string serviceName, string customArg)
        {
            try
            {
                var savedState = new Hashtable();

                var installContext = new InstallContext(Path.ChangeExtension(ExecutablePath, ".InstallLog"), new[] { string.Empty });
                installContext.Parameters["assemblypath"] = string.IsNullOrEmpty(customArg) ? ExecutablePath :
                                                            ExecutablePath + " \"" + customArg + "\"";

                var hostServiceInstaller = new HostServiceInstaller(serviceName)
                {
                    Context = installContext
                };
                hostServiceInstaller.Install(savedState);
                hostServiceInstaller.Commit(savedState);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Service install failed:");
                Console.WriteLine(ex);
                Console.ResetColor();
            }
        }