コード例 #1
0
ファイル: HostStarter.cs プロジェクト: zaharvsb52/MyWmsPro
        private ServiceController CreateService()
        {
            var service = ServiceController.GetServices().FirstOrDefault(con => con.ServiceName == ServiceName);

            if (service != null)
            {
                throw new DeveloperException("Service '{0}' already installed", ServiceName);
            }

            _log.DebugFormat("Try to install service '{0}'", ServiceName);

            var parameters = string.Join(" ", Parameters.Select(i => "-" + i.Key + (i.Value != null ? "=" + i.Value : string.Empty)));
            var appPath    = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var svcPath    = string.Format("\"{0}\\{1}.exe\" {2}", appPath, AppName, parameters);

            if (!DirectServiceInstaller.InstallService(svcPath, ServiceName, DisplayName, Description, true, false))
            {
                throw new DeveloperException("Can't install service. Check user rights for this operation.");
            }

            // проверяем, что сервис установился
            service = ServiceController.GetServices().FirstOrDefault(con => con.ServiceName == ServiceName);
            if (service == null)
            {
                throw new DeveloperException("Can't find just installed service " + ServiceName);
            }

            _log.DebugFormat("Service '{0}' successfully installed", ServiceName);
            return(service);
        }
コード例 #2
0
ファイル: HostStarter.cs プロジェクト: zaharvsb52/MyWmsPro
        private void RemoveService()
        {
            var service = ServiceController.GetServices().FirstOrDefault(con => con.ServiceName == ServiceName);

            if (service == null)
            {
                _log.WarnFormat("Can't stop service '{0}'. Service not found", ServiceName);
                return;
            }

            // если работает - останавливаем
            if (service.Status == ServiceControllerStatus.Running)
            {
                service.Stop();
            }

            // удаляем
            _log.InfoFormat(
                DirectServiceInstaller.UnInstallService(ServiceName)
                    ? "Service '{0}' successfully removed"
                    : "Can't remove '{0}' service. Unknown error in ServiceInstaller", ServiceName);
        }