码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作windows服务

时间:2018-02-04 18:07:25      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:服务安装   sep   gap   exist   说明   双引号   tco   cto   get   

本文主要说明了Windows服务的安装、卸载、启动、停止、获取服务安装路径、获取服务状态、检查服务是否存在、获取服务版本。

我们需要引用命名空间
using System.Configuration.Install;
using System.ServiceProcess;

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

C#操作windows服务

标签:服务安装   sep   gap   exist   说明   双引号   tco   cto   get   

原文地址:https://www.cnblogs.com/twinhead/p/8413496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!