ServiceHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Configuration.Install;
  4. using System.ServiceProcess;
  5. namespace Vinno.FIS.Sonopost.Upgrade
  6. {
  7. public class ServiceHelper
  8. {
  9. private static string _serviceFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\Vinno.FIS.Sonopost.Service.exe";
  10. private static string _serviceName = "SonopostService";
  11. /// <summary>
  12. /// 安装服务
  13. /// </summary>
  14. public static void Install()
  15. {
  16. if (IsServiceExisted(_serviceName))
  17. {
  18. UnInstall();
  19. }
  20. InstallService(_serviceFilePath);
  21. }
  22. /// <summary>
  23. /// 卸载服务
  24. /// </summary>
  25. public static void UnInstall()
  26. {
  27. if (IsServiceExisted(_serviceName))
  28. {
  29. ServiceStop(_serviceName);
  30. UninstallService(_serviceFilePath);
  31. }
  32. }
  33. /// <summary>
  34. /// 启动服务
  35. /// </summary>
  36. public static void Start()
  37. {
  38. if (IsServiceExisted(_serviceName))
  39. {
  40. ServiceStart(_serviceName);
  41. }
  42. }
  43. /// <summary>
  44. /// 停止服务
  45. /// </summary>
  46. public static void Stop()
  47. {
  48. if (IsServiceExisted(_serviceName))
  49. {
  50. ServiceStop(_serviceName);
  51. }
  52. }
  53. /// <summary>
  54. /// 服务是否已经启动
  55. /// </summary>
  56. /// <param name="serviceName"></param>
  57. /// <returns></returns>
  58. public static bool IsServiceStarted(string serviceName)
  59. {
  60. using (ServiceController control = new ServiceController(serviceName))
  61. {
  62. return control.Status == ServiceControllerStatus.Running;
  63. }
  64. }
  65. private static bool IsServiceExisted(string serviceName)
  66. {
  67. ServiceController[] services = ServiceController.GetServices();
  68. foreach (ServiceController sc in services)
  69. {
  70. if (sc.ServiceName.ToLower() == serviceName.ToLower())
  71. {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. private static void InstallService(string serviceFilePath)
  78. {
  79. using (AssemblyInstaller installer = new AssemblyInstaller())
  80. {
  81. installer.UseNewContext = true;
  82. installer.Path = serviceFilePath;
  83. IDictionary savedState = new Hashtable();
  84. installer.Install(savedState);
  85. installer.Commit(savedState);
  86. }
  87. }
  88. private static void UninstallService(string serviceFilePath)
  89. {
  90. using (AssemblyInstaller installer = new AssemblyInstaller())
  91. {
  92. installer.UseNewContext = true;
  93. installer.Path = serviceFilePath;
  94. installer.Uninstall(null);
  95. }
  96. }
  97. private static void ServiceStart(string serviceName)
  98. {
  99. using (ServiceController control = new ServiceController(serviceName))
  100. {
  101. if (control.Status == ServiceControllerStatus.Stopped)
  102. {
  103. control.Start();
  104. control.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
  105. }
  106. }
  107. }
  108. private static void ServiceStop(string serviceName)
  109. {
  110. using (ServiceController control = new ServiceController(serviceName))
  111. {
  112. if (control.Status == ServiceControllerStatus.Running)
  113. {
  114. control.Stop();
  115. control.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
  116. }
  117. }
  118. }
  119. }
  120. }