示例#1
0
        public bool InstallService()
        {
            string processPath = Application.ExecutablePath;

            try {
                TransactedInstaller     tranInstaller    = new TransactedInstaller();
                WindowsServiceInstaller serviceInstaller = new WindowsServiceInstaller(_serviceName, _serviceDescription, _startType);

                tranInstaller.Installers.Add(serviceInstaller);
                string[]       cmd     = { "/assemblypath=" + processPath };
                InstallContext context = new InstallContext(null, cmd);
                tranInstaller.Context = context;

                //delete the source if it's already there, installation failes if the source is already there
                if (EventLog.SourceExists(_serviceName))
                {
                    EventLog.DeleteEventSource(_serviceName);
                }

                tranInstaller.Install(new Hashtable());
            } catch (Exception e) {
                _logger.Error("Install failed for service " + _serviceName, e);
                return(false);
            }

            return(true);
        }
示例#2
0
        public bool UninstallService()
        {
            string processPath = Application.ExecutablePath;

            try {
                TransactedInstaller     tranInstaller    = new TransactedInstaller();
                WindowsServiceInstaller serviceInstaller = new WindowsServiceInstaller(_serviceName, _serviceDescription, _startType);

                tranInstaller.Installers.Add(serviceInstaller);
                string[]       cmd     = { "/assemblypath=" + processPath };
                InstallContext context = new InstallContext(null, cmd);
                tranInstaller.Context = context;

                tranInstaller.Uninstall(null);
            } catch (Exception e) {
                _logger.Error("Uninstall failed for service : " + _serviceName, e);
                return(false);
            }

            return(true);
        }