1. 程式人生 > >C#操作windows服務

C#操作windows服務

clas gist 啟動 filepath dir getname 服務安裝 repl empty

本文主要說明了Windows服務的安裝、卸載、啟動、停止、獲取服務安裝路徑、獲取服務狀態、檢查服務是否存在、獲取服務版本。

我們需要引用命名空間
using System.Configuration.Install;
using System.ServiceProcess;

  1. /// <summary>
  2. /// 獲取服務安裝路徑
  3. /// </summary>
  4. /// <param name="ServiceName"></param>
  5. /// <returns></returns>
  6. public static string GetWindowsServiceInstallPath(string ServiceName)
  7. {
  8. string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
  9. string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();
  10. //替換掉雙引號
  11. path = path.Replace("\"", string.Empty);
  12. FileInfo fi = new FileInfo(path);
  13. return fi.Directory.ToString()+"\\";
  14. }
  15. /// <summary>
  16. /// 根據服務名稱獲取服務狀態。
  17. /// </summary>
  18. /// <param name="serviceName">服務名</param>
  19. /// <returns>狀態</returns>
  20. public static EnumServiceStatus GetServiceStatus(string serviceName)
  21. {
  22. EnumServiceStatus status=EnumServiceStatus.CannotGet;
  23. System.ServiceProcess.ServiceController[] scs;
  24. scs = System.ServiceProcess.ServiceController.GetServices();
  25. foreach (System.ServiceProcess.ServiceController sc in scs)
  26. {
  27. if (serviceName.ToUpper() == sc.ServiceName.ToUpper())
  28. {
  29. switch (sc.Status)
  30. {
  31. case System.ServiceProcess.ServiceControllerStatus.Stopped:
  32. status = EnumServiceStatus.Stopped;
  33. break;
  34. case System.ServiceProcess.ServiceControllerStatus.StartPending:
  35. status = EnumServiceStatus.StartPending;
  36. break;
  37. case System.ServiceProcess.ServiceControllerStatus.Running:
  38. status = EnumServiceStatus.Running;
  39. break;
  40. case System.ServiceProcess.ServiceControllerStatus.StopPending:
  41. status = EnumServiceStatus.StopPending;
  42. break;
  43. case System.ServiceProcess.ServiceControllerStatus.PausePending:
  44. status = EnumServiceStatus.PausePending;
  45. break;
  46. case System.ServiceProcess.ServiceControllerStatus.Paused:
  47. status = EnumServiceStatus.Paused;
  48. break;
  49. case System.ServiceProcess.ServiceControllerStatus.ContinuePending:
  50. status = EnumServiceStatus.ContinuePending;
  51. break;
  52. }
  53. break;
  54. }
  55. }
  56. return status;
  57. }
  58. /// <summary>
  59. /// 檢查服務存在的存在性
  60. /// </summary>
  61. /// <param name=" NameService ">服務名</param>
  62. /// <returns>存在返回 true,否則返回 false;</returns>
  63. public static bool isServiceIsExisted(string NameService)
  64. {
  65. ServiceController[] services = ServiceController.GetServices();
  66. foreach (ServiceController s in services)
  67. {
  68. if (s.ServiceName.ToLower() == NameService.ToLower())
  69. {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. /// <summary>
  76. /// 安裝Windows服務
  77. /// </summary>
  78. /// <param name="stateSaver">集合</param>
  79. /// <param name="filepath">程序文件路徑</param>
  80. public static void InstallmyService(IDictionary stateSaver, string filepath)
  81. {
  82. AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
  83. AssemblyInstaller1.UseNewContext = true;
  84. AssemblyInstaller1.Path = filepath;
  85. AssemblyInstaller1.Install(stateSaver);
  86. AssemblyInstaller1.Commit(stateSaver);
  87. AssemblyInstaller1.Dispose();
  88. }
  89. /// <summary>
  90. /// 卸載Windows服務
  91. /// </summary>
  92. /// <param name="filepath">程序文件路徑</param>
  93. public static void UnInstallmyService(string filepath)
  94. {
  95. AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
  96. AssemblyInstaller1.UseNewContext = true;
  97. AssemblyInstaller1.Path = filepath;
  98. AssemblyInstaller1.Uninstall(null);
  99. AssemblyInstaller1.Dispose();
  100. }
  101. /// <summary>
  102. /// 啟動服務
  103. /// </summary>
  104. /// <param name=" NameService ">服務名</param>
  105. /// <returns>存在返回 true,否則返回 false;</returns>
  106. public static bool RunService(string NameService)
  107. {
  108. bool bo = true;
  109. try
  110. {
  111. ServiceController sc = new ServiceController(NameService);
  112. if (sc.Status.Equals(ServiceControllerStatus.Stopped)||sc.Status.Equals(ServiceControllerStatus.StopPending))
  113. {
  114. sc.Start();
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. bo = false;
  120. LogAPI.WriteLog(ex.Message);
  121. }
  122. return bo;
  123. }
  124. /// <summary>
  125. /// 停止服務
  126. /// </summary>
  127. /// <param name=" NameService ">服務名</param>
  128. /// <returns>存在返回 true,否則返回 false;</returns>
  129. public static bool StopService(string NameService)
  130. {
  131. bool bo = true;
  132. try
  133. {
  134. ServiceController sc = new ServiceController(NameService);
  135. if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
  136. {
  137. sc.Stop();
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. bo = false;
  143. LogAPI.WriteLog(ex.Message);
  144. }
  145. return bo;
  146. }
  147. /// <summary>
  148. /// 獲取指定服務的版本號
  149. /// </summary>
  150. /// <param name="serviceName">服務名稱</param>
  151. /// <returns></returns>
  152. public static string GetServiceVersion(string serviceName)
  153. {
  154. if (string.IsNullOrEmpty(serviceName))
  155. {
  156. return string.Empty;
  157. }
  158. try
  159. {
  160. string path = GetWindowsServiceInstallPath(serviceName) + "\\" + serviceName + ".exe";
  161. Assembly assembly = Assembly.LoadFile(path);
  162. AssemblyName assemblyName = assembly.GetName();
  163. Version version = assemblyName.Version;
  164. return version.ToString();
  165. }
  166. catch (Exception ex)
  167. {
  168. LogAPI.WriteLog(ex.Message);
  169. return string.Empty;
  170. }
  171. //string a = version.Major.ToString();
  172. //string b = version.Minor.ToString();
  173. //string c = version.Revision.ToString();
  174. //string d = version.Build.ToString();
  175. }

C#操作windows服務