123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections;
- using System.Configuration.Install;
- using System.IO;
- using System.ServiceProcess;
- namespace Vinno.FIS.Sonopost.Upgrade
- {
- public class ServiceHelper
- {
- private static readonly string _serviceFilePath = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1)).FullName, "Vinno.FIS.Sonopost.Service.exe");
- private static readonly string _serviceName = "SonopostService";
- /// <summary>
- /// 安装服务
- /// </summary>
- public static void Install()
- {
- if (IsServiceExisted(_serviceName))
- {
- UnInstall();
- }
- InstallService(_serviceFilePath);
- }
- /// <summary>
- /// 卸载服务
- /// </summary>
- public static void UnInstall()
- {
- if (IsServiceExisted(_serviceName))
- {
- ServiceStop(_serviceName);
- UninstallService(_serviceFilePath);
- }
- }
- /// <summary>
- /// 启动服务
- /// </summary>
- public static void Start()
- {
- if (IsServiceExisted(_serviceName))
- {
- ServiceStart(_serviceName);
- }
- }
- /// <summary>
- /// 停止服务
- /// </summary>
- public static void Stop()
- {
- if (IsServiceExisted(_serviceName))
- {
- ServiceStop(_serviceName);
- }
- }
- /// <summary>
- /// 服务是否已经启动
- /// </summary>
- /// <param name="serviceName"></param>
- /// <returns></returns>
- public static bool IsServiceStarted(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- return control.Status == ServiceControllerStatus.Running;
- }
- }
- private static bool IsServiceExisted(string serviceName)
- {
- ServiceController[] services = ServiceController.GetServices();
- foreach (ServiceController sc in services)
- {
- if (sc.ServiceName.ToLower() == serviceName.ToLower())
- {
- return true;
- }
- }
- return false;
- }
- private static void InstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- IDictionary savedState = new Hashtable();
- installer.Install(savedState);
- installer.Commit(savedState);
- }
- }
- private static void UninstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- private static void ServiceStart(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Stopped)
- {
- control.Start();
- control.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
- }
- }
- }
- private static void ServiceStop(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Running)
- {
- control.Stop();
- control.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
- }
- }
- }
- }
- }
|