Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //// 运行服务
            //if (args.Length == 0)
            //{
            //    ServiceBase[] ServicesToRun;
            //    ServicesToRun = new ServiceBase[]
            //    {
            //        new NTService()
            //    };
            //    ServiceBase.Run(ServicesToRun);
            //}
            //// 安装服务
            //else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
            //{
            //try
            //{
            //    string[] cmdline = { };
            //    string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //    TransactedInstaller transactedInstaller = new TransactedInstaller();
            //    AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
            //    transactedInstaller.Installers.Add(assemblyInstaller);
            //    transactedInstaller.Install(new System.Collections.Hashtable());

            //}
            //catch (Exception ex)
            //{
            //    string msg = ex.Message;
            //}
            //}
            //// 删除服务
            //else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
            //{
            try
            {
                string[]            cmdline             = { };
                string              serviceFileName     = System.Reflection.Assembly.GetExecutingAssembly().Location;
                TransactedInstaller transactedInstaller = new TransactedInstaller();
                AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
                transactedInstaller.Installers.Add(assemblyInstaller);
                transactedInstaller.Uninstall(null);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            //}
        }
    public static void Main()
    {
        try
        {
// <Snippet1>
            TransactedInstaller myTransactedInstaller = new TransactedInstaller();
            AssemblyInstaller   myAssemblyInstaller;
            InstallContext      myInstallContext;

            // Create a instance of 'AssemblyInstaller' that installs 'MyAssembly1.exe'.
            myAssemblyInstaller =
                new AssemblyInstaller("MyAssembly1.exe", null);

            // Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
            myTransactedInstaller.Installers.Add(myAssemblyInstaller);

            // Create a instance of 'AssemblyInstaller' that installs 'MyAssembly2.exe'.
            myAssemblyInstaller =
                new AssemblyInstaller("MyAssembly2.exe", null);

            // Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
            myTransactedInstaller.Installers.Add(myAssemblyInstaller);

            //Print the assemblies to be installed.
            InstallerCollection myInstallers = myTransactedInstaller.Installers;
            Console.WriteLine("\nPrinting all assemblies to be installed");
            for (int i = 0; i < myInstallers.Count; i++)
            {
                if ((myInstallers[i].GetType()).Equals(typeof(AssemblyInstaller)))
                {
                    Console.WriteLine("{0} {1}", i + 1,
                                      ((AssemblyInstaller)myInstallers[i]).Path);
                }
            }
// </Snippet1>
            // Create a instance of 'InstallContext' with log file named 'Install.log'.
            myInstallContext =
                new InstallContext("Install.log", null);
            myTransactedInstaller.Context = myInstallContext;

            // Install an assembly .
            myTransactedInstaller.Install(new Hashtable());
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception raised : {0}", e.Message);
        }
    }
Ejemplo n.º 3
0
        public static bool Install <T>(string path, string[] commandLine) where T : Installer, new()
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            var install   = CommandLine.Contains(commandLine, CommandLine.Install);
            var uninstall = CommandLine.Contains(commandLine, CommandLine.Uninstall);

            if (!install && !uninstall)
            {
                return(false);
            }

            if (install && uninstall)
            {
                throw new InvalidOperationException("install && uninstall");
            }

            using (var installer = new T())
            {
                var transacted = new TransactedInstaller()
                {
                    Context = new InstallContext(null, commandLine)
                    {
                        Parameters = { { AssemblyPathContextKey, path } }
                    },

                    Installers = { installer }
                };

                using (transacted)
                {
                    if (install)
                    {
                        var state = new Hashtable();
                        transacted.Install(state);
                    }
                    else
                    {
                        transacted.Uninstall(null);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method starts the service.
        /// </summary>
        static void Main(string[] args)
        {
            // To run more than one service you have to add them here
            //ServiceBase.Run(new ServiceBase[] { new WatchService() });
            // 运行服务
            if (args.Length == 0)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new WatchService() };
                ServiceBase.Run(ServicesToRun);
            }
            // 安装服务
            else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
            {
                try
                {
                    string[] cmdline         = { };
                    string   serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Install(new System.Collections.Hashtable());
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
            // 删除服务
            else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
            {
                try
                {
                    string[] cmdline         = { };
                    string   serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Uninstall(null);
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
        }
Ejemplo n.º 5
0
        public void ExecuteInternal(HostArguments args)
        {
            var serviceInstaller = new ServiceInstaller
            {
                ServiceName = args.ServiceName,
                Description = args.Description,
                DisplayName = args.DisplayName,
            };

            SetStartMode(serviceInstaller, args.StartMode);

            var serviceProcessInstaller = new ServiceProcessInstaller
            {
                Username = args.Username,
                Password = args.Password,
                Account  = args.ServiceAccount,
            };
            var installers = new Installer[]
            {
                serviceInstaller,
                serviceProcessInstaller
            };

            var arguments = string.Empty;

            if (!string.IsNullOrEmpty(args.Url))
            {
                arguments += string.Format(" --url=\"{0}\"", args.Url);
            }

            using (var hostInstaller = new HostInstaller(args, arguments, installers))
                using (var transactedInstaller = new TransactedInstaller())
                {
                    transactedInstaller.Installers.Add(hostInstaller);

                    var assembly = Assembly.GetEntryAssembly();

                    var      path        = string.Format("/assemblypath={0}", assembly.Location);
                    string[] commandLine = { path };

                    var context = new InstallContext(null, commandLine);
                    transactedInstaller.Context = context;

                    Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

                    action(transactedInstaller);
                }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 安装windowservice
        /// </summary>
        /// <param name="serviceName">WindowService名称</param>
        /// <param name="serviceProgramFileFullPath">服务的执行程序完整路径</param>
        /// <param name="windowServiceStartType"></param>
        /// <returns>成功返回 true,否则返回 false;</returns>
        public bool InstallWinService(string serviceName, string serviceProgramFileFullPath, int windowServiceStartType)
        {
            if (CheckWinServiceIsExisted(serviceName))
            {
                return(true);
            }
            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceProgramFileFullPath, new string[] { });

            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Install(new Hashtable());
            assemblyInstaller.Dispose();
            transactedInstaller.Dispose();
            ChangeWinServiceStartType(serviceName, windowServiceStartType);
            return(CheckWinServiceIsExisted(serviceName));
        }
Ejemplo n.º 7
0
        public static void Install(string args)
        {
            TransactedInstaller ti = new TransactedInstaller();
            SvcInstaller        mi = new SvcInstaller();

            ti.Installers.Add(mi);
            String path = String.Format("/assemblypath={0}", Assembly.GetExecutingAssembly().Location);

            String[] cmdline = { path };
            Console.WriteLine("Installing at path " + path + " with args " + args);
            InstallContext ctx = new InstallContext("", cmdline);

            //ctx.Parameters["assemblypath"] += "\" " + args;
            ti.Context = ctx;
            ti.Install(new System.Collections.Hashtable());
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 卸载Windows服务
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <param name="serviceFileName">服务文件路径</param>
 public static bool UnInstallService(string serviceName, string serviceFileName)
 {
     if (ServiceIsExisted(serviceName))
     {
         string[]            cmdline             = { };
         TransactedInstaller transactedInstaller = new TransactedInstaller();
         AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
         transactedInstaller.Installers.Add(assemblyInstaller);
         transactedInstaller.Uninstall(null);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 9
0
        private void Dispose(bool disposing)
        {
            if (disposing && IsInstalled)
            {
                TransactedInstaller installer = PrepareInstaller();

                try
                {
                    installer.Uninstall(null);
                } finally
                {
                    IsInstalled = false;
                };
            }
            ;
        }
Ejemplo n.º 10
0
        private static Installer CreateInstaller(string serviceName)
        {
            var installer = new TransactedInstaller();

            installer.Installers.Add(new ServiceInstaller
            {
                ServiceName = serviceName,
                DisplayName = serviceName,
                StartType   = ServiceStartMode.Automatic
            });
            installer.Installers.Add(new ServiceProcessInstaller
            {
                Account = ServiceAccount.LocalSystem
            });
            return(installer);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 卸载windowservice
        /// </summary>
        /// <param name="serviceName">WindowService名称</param>
        /// <param name="serviceProgramFullPath">服务的执行程序完整路径</param>
        /// <returns>成功返回 true,否则返回 false;</returns>
        public static bool UninstallService(string serviceName, string serviceProgramFullPath)
        {
            if (!IsServiceIsExisted(serviceName))
            {
                return(true);
            }
            string[]            cmdline             = {};
            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceProgramFullPath, cmdline);

            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Uninstall(null);
            assemblyInstaller.Dispose();
            transactedInstaller.Dispose();
            return(!IsServiceIsExisted(serviceName));
        }
        public void Install()
        {
            string serviceName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GTaskService.exe");

            if (!File.Exists(serviceName))
            {
                Console.WriteLine("TaskService.exe文件不存在!");
                return;
            }
            try {
                string[] cmdline = { };

                TransactedInstaller t = new TransactedInstaller();
            }
            catch (Exception ex)
            { }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="serviceFileName">服务文件路径</param>
        /// <returns></returns>
        public static bool InstallService(string serviceName, string serviceFileName)
        {
            if (!ServiceIsExisted(serviceName))
            {
                string[]            cmdline             = { };
                TransactedInstaller transactedInstaller = new TransactedInstaller();
                AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
                transactedInstaller.Installers.Add(assemblyInstaller);
                transactedInstaller.Install(new System.Collections.Hashtable());

                return(true);
            }
            else
            {
                throw new Exception(string.Format("{0} 服务已经存在", serviceName));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string serviceFileName = (string)e.Argument;

            string[]         cmdline = { };
            BackgroundWorker bw      = (BackgroundWorker)sender;

            bw.ReportProgress(0, "正在卸载服务,请稍等....");


            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);

            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Uninstall(null);
            bw.ReportProgress(0, "服务卸载成功!");
        }
Ejemplo n.º 15
0
 /// <summary>
 /// 安装自身
 /// </summary>
 public static void InStallSelf()
 {
     System.Collections.Hashtable installcode = new System.Collections.Hashtable();
     using (TransactedInstaller ti = new TransactedInstaller())
     {
         try
         {
             AssemblyInstaller ass = new AssemblyInstaller(Assembly.GetExecutingAssembly().Location, new String[0]);
             ti.Installers.Add(ass);
             ti.Install(installcode);
         }
         catch
         {
             ti.Rollback(installcode);
         }
     }
 }
Ejemplo n.º 16
0
        /// <summary>
        ///         Register the Windows service.
        /// </summary>
        /// <param name="serviceUser">Service user</param>
        /// <param name="servicePassword">Service password</param>
        static void InstallService(string serviceUser, string servicePassword)
        {
            if (String.IsNullOrWhiteSpace(serviceUser))
            {
                throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'serviceUser'.", "serviceUser");
            }

            if (String.IsNullOrWhiteSpace(servicePassword))
            {
                throw new ArgumentException("Argument cannot be null, empty, or composed entirely of whitespace: 'servicePassword'.", "servicePassword");
            }

            InstallContext installContext = CreateInstallContext();

            using (TransactedInstaller installer = CreateInstaller(installContext, serviceUser, servicePassword))
            {
                if (!EventLog.SourceExists(PowerShellRestApiService.EventLogSourceName))
                {
                    EventLog.CreateEventSource(PowerShellRestApiService.EventLogSourceName, "Application");
                }

                Hashtable stateStore = new Hashtable();
                installer.Install(stateStore);

                string storeFilePath =
                    Path.Combine(
                        // ReSharper disable once AssignNullToNotNullAttribute
                        Path.GetDirectoryName(
                            typeof(Program)
                            .Assembly
                            .Location
                            ),
                        "DpsApiMonitorServiceInstallState.bin"
                        );
                if (File.Exists(storeFilePath))
                {
                    File.Delete(storeFilePath);
                }

                using (FileStream storeStream = File.Create(storeFilePath))
                {
                    new BinaryFormatter()
                    .Serialize(storeStream, stateStore);
                }
            }
        }
Ejemplo n.º 17
0
        internal static void RunInstaller(bool install, string serviceName)
        {
            var tinstaller  = new TransactedInstaller();
            var cfinstaller = new SecureChatServiceInstaller(serviceName);

            tinstaller.Installers.Add(cfinstaller);
            tinstaller.Context = new InstallContext("", new[] { string.Format("/assemblypath={0}", Assembly.GetExecutingAssembly().Location) });
            ;
            if (install)
            {
                tinstaller.Install(new Hashtable());
            }
            else
            {
                tinstaller.Uninstall(null);
            }
        }
Ejemplo n.º 18
0
        public static void Uninstall(string[] args)
        {
            string name = args.Length == 2 ? args[1] : DEFAULT_NAME;

            try {
                TransactedInstaller            ti = new TransactedInstaller();
                WindowsServiceProjectInstaller mi = WindowsServiceProjectInstaller.Create(name);
                ti.Installers.Add(mi);
                string         path    = string.Format("/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location);
                string[]       cmdline = { path };
                InstallContext ctx     = new InstallContext("", cmdline);
                ti.Context = ctx;
                ti.Uninstall(null);
            }
            //Swallow exception when we're trying to uninstall non-existent service
            catch { }
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            ServiceController service = new ServiceController("WCFServiceMgr");

            if (args.Length == 0)
            {
                ServiceBase[] serviceRun;
                serviceRun = new ServiceBase[] { new WCFServiceMgr() };
                ServiceBase.Run(serviceRun);
            }
            else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
            {
                if (!IsServiceExisted("WCFServiceMgr"))
                {
                    try
                    {
                        string[]            cmdLine      = { };
                        string              exeLocalhost = System.Reflection.Assembly.GetExecutingAssembly().Location;
                        TransactedInstaller install      = new TransactedInstaller();
                        AssemblyInstaller   assmeInstall = new AssemblyInstaller(exeLocalhost, cmdLine);
                        install.Installers.Add(assmeInstall);

                        install.Install(new System.Collections.Hashtable());
                        TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
                        service.Start(args);
                        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
            {
                string[]            cmdLine     = { };
                string              exeFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                TransactedInstaller install     = new TransactedInstaller();
                AssemblyInstaller   assmInstall = new AssemblyInstaller(exeFileName, cmdLine);
                install.Installers.Add(assmInstall);
                install.Uninstall(null);
                service.Start(args);
                service.WaitForStatus(ServiceControllerStatus.Stopped);
            }
        }
Ejemplo n.º 20
0
        private void DoUninstall(string serviceName)
        {
            _logger.LogInformation("Uninstall: Preparing");

            var isMissingParam = false;

            if (string.IsNullOrWhiteSpace(serviceName))
            {
                _logger.LogWarning("Missing parameter : [/serviceName <value>]");
                isMissingParam = true;
            }

            if (isMissingParam)
            {
                _logger.LogError("Uninstall : Aborting due to missing parameters");
                return;
            }

            var exeName = Process.GetCurrentProcess().MainModule.FileName;

            _logger.LogInformation($"Uninstall: Parameters [serviceName:{serviceName}] [path:{exeName}]");

            var installer = new TransactedInstaller();
            var pi        = new ServiceInstaller(string.Empty, serviceName, string.Empty);

            installer.Installers.Add(pi);
            var path    = $"/assemblypath={exeName}";
            var context = new InstallContext("install.log", new[] { path });

            context.LogMessage("Using path " + path);

            installer.Context = context;

            try
            {
                installer.Uninstall(null);
            }
            catch (Exception e)
            {
                _logger.LogExceptionEx(e, e.Message, ("serviceName", serviceName));
            }


            _logger.LogInformation("Uninstall : Done");
        }
Ejemplo n.º 21
0
        private static void UnInstallService()
        {
            string serviceFileName = "";

            try
            {
                serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
                TransactedInstaller transactedInstaller = new TransactedInstaller();
                AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, null);
                transactedInstaller.Installers.Add(assemblyInstaller);
                transactedInstaller.Uninstall(null);
                Log.Debug($"{serviceFileName} uninstall success !");
            }
            catch (Exception ex)
            {
                Log.Error($"{serviceFileName} uninstall error ${ex.Message}!");
            }
        }
Ejemplo n.º 22
0
 public void Uninstall(string[] cmdArgs)
 {
     if (!IsAdministrator())
     {
         RunAsAdministrator(string.Join(" ", cmdArgs));
     }
     else
     {
         try
         {
             TransactedInstaller transactedInstaller = new TransactedInstaller();
             AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(Application.ExecutablePath, new string[] { });
             transactedInstaller.Installers.Add(assemblyInstaller);
             transactedInstaller.Uninstall(null);
         }
         catch (Exception ex) { TraceLog.WriteException(ex); throw; }
     }
 }
Ejemplo n.º 23
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            string opt = null;

            if (args.Length >= 1)
            {
                opt = args[0];
            }

            if (opt != null && opt.ToUpperInvariant() == "/INSTALL")
            {
                TransactedInstaller ti = new TransactedInstaller();
                ProjectInstaller    mi = new ProjectInstaller();
                ti.Installers.Add(mi);
                String path = String.Format("/assemblypath={0}",
                                            System.Reflection.Assembly.GetExecutingAssembly().Location);
                String[]       cmdline = { path };
                InstallContext ctx     = new InstallContext("", cmdline);
                ti.Context = ctx;
                ti.Install(new Hashtable());
                return;
            }

            if (opt != null && opt.ToUpperInvariant() == "/UNINSTALL")
            {
                TransactedInstaller ti = new TransactedInstaller();
                ProjectInstaller    mi = new ProjectInstaller();
                ti.Installers.Add(mi);
                String path = String.Format("/assemblypath={0}",
                                            System.Reflection.Assembly.GetExecutingAssembly().Location);
                String[]       cmdline = { path };
                InstallContext ctx     = new InstallContext("", cmdline);
                ti.Context = ctx;
                ti.Uninstall(null);
                return;
            }

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new WatchDogService()
            };
            ServiceBase.Run(ServicesToRun);
        }
Ejemplo n.º 24
0
    public static void Main()
    {
// <Snippet1>
        TransactedInstaller myTransactedInstaller = new TransactedInstaller();
        AssemblyInstaller   myAssemblyInstaller;
        InstallContext      myInstallContext;

        // Create an instance of 'AssemblyInstaller' that installs 'MyAssembly1.exe'.
        myAssemblyInstaller =
            new AssemblyInstaller("MyAssembly1.exe", null);

        // Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
        myTransactedInstaller.Installers.Add(myAssemblyInstaller);

        // Create an instance of 'AssemblyInstaller' that installs 'MyAssembly2.exe'.
        myAssemblyInstaller =
            new AssemblyInstaller("MyAssembly2.exe", null);

        // Add the instance of 'AssemblyInstaller' to the 'TransactedInstaller'.
        myTransactedInstaller.Installers.Add(myAssemblyInstaller);

        Installer[] myInstallers =
            new Installer[myTransactedInstaller.Installers.Count];

        myTransactedInstaller.Installers.CopyTo(myInstallers, 0);
        // Print the assemblies to be installed.
        Console.WriteLine("Printing all assemblies to be installed -");
        for (int i = 0; i < myInstallers.Length; i++)
        {
            if ((myInstallers[i].GetType()).Equals(typeof(AssemblyInstaller)))
            {
                Console.WriteLine("{0} {1}", i + 1,
                                  ((AssemblyInstaller)myInstallers[i]).Path);
            }
        }
// </Snippet1>
        // Create a instance of 'InstallContext' with log file named 'Install.log'.
        myInstallContext =
            new InstallContext("Install.log", null);
        myTransactedInstaller.Context = myInstallContext;

        // Install an assembly.
        myTransactedInstaller.Install(new Hashtable());
    }
Ejemplo n.º 25
0
 private void tbInstall_Click(object sender, EventArgs e)
 {
     try
     {
         string[]            cmdline             = { };
         string              serviceFileName     = System.Reflection.Assembly.GetExecutingAssembly().Location;
         TransactedInstaller transactedInstaller = new TransactedInstaller();
         AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceFileName, cmdline);
         transactedInstaller.Installers.Add(assemblyInstaller);
         transactedInstaller.Install(new System.Collections.Hashtable());
         MessageBox.Show("Install success.\r\n\r\n please close application and start service by Windows services manager or reboot system.", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
         tbInstall.Enabled   = false;
         tbUninstall.Enabled = true;
     }
     catch (Exception ex)
     {
         MessageBox.Show("Install error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Ejemplo n.º 26
0
        public static void Install(string[] args)
        {
            string name = args.Length == 2 ? args[1] : DEFAULT_NAME;

            try {
                TransactedInstaller            ti = new TransactedInstaller();
                WindowsServiceProjectInstaller pi = WindowsServiceProjectInstaller.Create(name);
                ti.Installers.Add(pi);
                string path = string.Format("/assemblypath={0}",
                                            System.Reflection.Assembly.GetExecutingAssembly().Location);
                string[]       cmdline = { path };
                InstallContext ctx     = new InstallContext("", cmdline);
                ti.Context = ctx;
                ti.Install(new Hashtable());
            } catch (Exception ex) {
                Console.WriteLine("ERROR: {0}", ex.Message);
                Environment.Exit(1);
            }
        }
Ejemplo n.º 27
0
 private static void PerformInstall()
 {
     try
     {
         TransactedInstaller ti = new TransactedInstaller();
         ProjectInstaller    mi = new ProjectInstaller();
         ti.Installers.Add(mi);
         String path = String.Format("/assemblypath={0}",
                                     System.Reflection.Assembly.GetExecutingAssembly().Location);
         String[]       cmdline = { path };
         InstallContext ctx     = new InstallContext("", cmdline);
         ti.Context = ctx;
         ti.Install(new Hashtable());
     }
     catch (Exception exc)
     {
         ExceptionHandler.Output("/install failed", exc);
     }
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Use to install windows service
        /// </summary>
        /// <param name="filePath">your serviceFilePath</param>
        /// <param name="runRight">is run</param>
        /// <returns></returns>
        public static bool InstallWindowsService(string filePath, string serviceName)
        {
            if (ServiceController.GetServices().Where(serviceItem => serviceItem.ServiceName.Equals(serviceName)).Count() == 0)
            {
                try
                {
                    AssemblyInstaller   asi           = new AssemblyInstaller(filePath, new string[] { });
                    TransactedInstaller tranInstaller = new TransactedInstaller();
                    tranInstaller.Installers.Add(asi);
                    tranInstaller.Install(new Hashtable());
                }
                catch
                {
                    return(false);
                }
            }

            return(true);
        }
        private static void InstallContext <T>(dynamic args = null) where T : IWindowsService
        {
            var path = "/assemblypath=" + Assembly.GetEntryAssembly().Location;

            using (var ti = new TransactedInstaller())
            {
                ti.Installers.Add(new WindowsServiceInstaller(typeof(T)));
                ti.Context = new InstallContext(null, new[] { path });

                if (args == null)
                {
                    ti.Uninstall(null);
                }
                else
                {
                    ti.Install(args);
                }
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        ///
        static void Main(string[] args)
        {
            string args0  = "";
            string args1  = "";
            string pfPath = Environment.GetEnvironmentVariable("ProgramFiles");

            if (args.Length > 0)
            {
                args0 = args[0];
            }

            if (args0.ToLower() == "-install")
            {
                //MessageBox.Show("PokeService Install", "Poke Service");
                TransactedInstaller ti = new TransactedInstaller();
                Installer1          pi = new Installer1();
                ti.Installers.Add(pi);

                InstallContext ctx = new InstallContext();
                ti.Context = ctx;
                ctx.Parameters.Add("assemblypath", pfPath + "\\Honsa Consulting\\PokeSrvice.exe");
                ti.Install(new Hashtable());
            }

            else if (args0.ToLower() == "-uninstall")
            {
                TransactedInstaller ti = new TransactedInstaller();
                Installer1          pi = new Installer1();
                ti.Installers.Add(pi);

                InstallContext ctx = new InstallContext();
                ctx.Parameters.Add("assemblypath", pfPath + "\\Honsa Consulting\\PokeSrvice.exe");
                ti.Uninstall(null);
            }
            else if (args0 != "")
            {
            }

            // start up the service
            System.ServiceProcess.ServiceBase[] ServiceToRun;
            ServiceToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
            System.ServiceProcess.ServiceBase.Run(ServiceToRun);
        }
Ejemplo n.º 31
0
 public static void Install(string[] args)
 {
     bool flag = false;
     bool flag2 = false;
     TransactedInstaller installerWithHelp = new TransactedInstaller();
     bool flag3 = false;
     try
     {
         ArrayList list = new ArrayList();
         for (int i = 0; i < args.Length; i++)
         {
             if (args[i].StartsWith("/", StringComparison.Ordinal) || args[i].StartsWith("-", StringComparison.Ordinal))
             {
                 string strA = args[i].Substring(1);
                 if ((string.Compare(strA, "u", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(strA, "uninstall", StringComparison.OrdinalIgnoreCase) == 0))
                 {
                     flag = true;
                 }
                 else if ((string.Compare(strA, "?", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(strA, "help", StringComparison.OrdinalIgnoreCase) == 0))
                 {
                     flag3 = true;
                 }
                 else if (string.Compare(strA, "AssemblyName", StringComparison.OrdinalIgnoreCase) == 0)
                 {
                     flag2 = true;
                 }
                 else
                 {
                     list.Add(args[i]);
                 }
             }
             else
             {
                 string name = args[i];
                 ServiceName = name;
                 Assembly assembly = Assembly.GetExecutingAssembly();
                 AssemblyInstaller installer2 = new AssemblyInstaller(assembly, (string[])list.ToArray(typeof(string)));
                 installerWithHelp.Installers.Add(installer2);
             }
         }
         if (flag3 || (installerWithHelp.Installers.Count == 0))
         {
             flag3 = true;
             installerWithHelp.Installers.Add(new AssemblyInstaller());
             throw new InvalidOperationException("GetHelp(installerWithHelp)");
         }
         installerWithHelp.Context = new InstallContext("InstallUtil.InstallLog", (string[])list.ToArray(typeof(string)));
     }
     catch (Exception exception2)
     {
         if (flag3)
         {
             throw exception2;
         }
         throw new InvalidOperationException("InstallInitializeException");
     }
     try
     {
         string str2 = installerWithHelp.Context.Parameters["installtype"];
         if ((str2 != null) && (string.Compare(str2, "notransaction", StringComparison.OrdinalIgnoreCase) == 0))
         {
             string str3 = installerWithHelp.Context.Parameters["action"];
             if ((str3 != null) && (string.Compare(str3, "rollback", StringComparison.OrdinalIgnoreCase) == 0))
             {
                 installerWithHelp.Context.LogMessage("InstallRollbackNtRun");
                 for (int j = 0; j < installerWithHelp.Installers.Count; j++)
                 {
                     installerWithHelp.Installers[j].Rollback(null);
                 }
             }
             else if ((str3 != null) && (string.Compare(str3, "commit", StringComparison.OrdinalIgnoreCase) == 0))
             {
                 installerWithHelp.Context.LogMessage("InstallCommitNtRun");
                 for (int k = 0; k < installerWithHelp.Installers.Count; k++)
                 {
                     installerWithHelp.Installers[k].Commit(null);
                 }
             }
             else if ((str3 != null) && (string.Compare(str3, "uninstall", StringComparison.OrdinalIgnoreCase) == 0))
             {
                 installerWithHelp.Context.LogMessage("InstallUninstallNtRun");
                 for (int m = 0; m < installerWithHelp.Installers.Count; m++)
                 {
                     installerWithHelp.Installers[m].Uninstall(null);
                 }
             }
             else
             {
                 installerWithHelp.Context.LogMessage("InstallInstallNtRun");
                 for (int n = 0; n < installerWithHelp.Installers.Count; n++)
                 {
                     installerWithHelp.Installers[n].Install(null);
                 }
             }
         }
         else if (!flag)
         {
             IDictionary stateSaver = new Hashtable();
             installerWithHelp.Install(stateSaver);
         }
         else
         {
             installerWithHelp.Uninstall(null);
         }
     }
     catch (Exception exception3)
     {
         throw exception3;
     }
 }