ServiceHelper.cs 4.0 KB

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