C#启动Windows服务及关闭实例实现

开发 后端
C#启动Windows服务及关闭实例实现是如何做到的呢?那么本文就向介绍C#启动Windows服务及关闭实例实现的具体的代码。

C#启动Windows服务及关闭实例实现的步骤和使用调用的类是什么呢?让我们来看看具体的实现:

C# 控制Windows服务的启动与关闭

  1. // ......  
  2.   using System.ServiceProcess;  
  3.  // ......  
  4.  /// <summary>  
  5. /// Restart windows service  
  6. /// </summary>  
  7. /// <param name="serviceName">  
  8. ///the windows service display name</param>  
  9. /// <returns> If the restart successfully  
  10. ///return true else return false</returns>  
  11. public static bool RestartWindowsService(string serviceName)  
  12. {  
  13.     bool bResult = false;  
  14.     try 
  15.     {  
  16. try 
  17. {  //C#启动Windows服务及关闭
  18.     StopWindowsService(serviceName);  
  19.     Thread.Sleep(1000);  
  20. }  
  21. catch (Exception ex)  
  22. {  
  23.     StartWindowsService(serviceName);  
  24.     Thread.Sleep(1000);  
  25.     StopWindowsService(serviceName);  
  26.     Thread.Sleep(1000);  
  27.     Console.WriteLine(ex.Message);  
  28. }  
  29. try 
  30. {  
  31.     StartWindowsService(serviceName);  
  32.     Thread.Sleep(1000);  
  33. }  
  34. catch (Exception ex)  //C#启动Windows服务及关闭
  35. {  
  36.     StopWindowsService(serviceName);  
  37.     Thread.Sleep(1000);  
  38.     StartWindowsService(serviceName);  
  39.     Thread.Sleep(1000);  
  40.     Console.WriteLine(ex.Message);  
  41. }  
  42. bResult = true;  
  43.     }  
  44.     catch (Exception ex)  
  45.     {  
  46. bResult = false;  
  47. throw ex;  
  48.     }  
  49.     return bResult;  
  50. }  
  51.  //C#启动Windows服务及关闭
  52. /// <summary>  
  53. /// Start windows service  
  54. /// </summary>  
  55. /// <param name="serviceName">  
  56. ///the windows service display name</param>  
  57. /// <returns>If the start successfully   
  58. ///return true else return false</returns>  
  59. public static bool StopWindowsService(string serviceName)  
  60. {  
  61.     ServiceController[] scs = ServiceController.GetServices();  
  62.     bool bResult = false;  
  63.     foreach (ServiceController sc in scs)  
  64.     {  
  65. if (sc.DisplayName == serviceName)  
  66. {  
  67.     try 
  68.     {  
  69. sc.WaitForStatus(ServiceControllerStatus.Running,   
  70. TimeSpan.FromSeconds(30));  
  71. sc.Stop();  
  72. bResult = true;  
  73.     }  
  74.     catch (Exception ex)  
  75.     {  
  76. bResult = false;  
  77. throw ex;  
  78.     }  
  79. }  
  80.     }  
  81.     return bResult;  
  82. }  
  83.  //C#启动Windows服务及关闭
  84. /// <summary>  
  85. /// Stop windows service  
  86. /// </summary>  
  87. /// <param name="serviceName">the windows  
  88. ///service display name</param>  
  89. /// <returns>If the stop successfully   
  90. ///return true else return false</returns>  
  91. public static bool StartWindowsService(  
  92. string serviceName)  
  93. {  
  94.     ServiceController[] scs  
  95.  = ServiceController.GetServices();  
  96.     bool bResult = false;  
  97.     foreach (ServiceController sc in scs)  
  98.     {  
  99. if (sc.DisplayName == serviceName)  
  100. {  
  101.     try 
  102.     {  
  103. sc.WaitForStatus(ServiceControllerStatus.Stopped,   
  104. TimeSpan.FromSeconds(30));  
  105. sc.Start();  
  106. bResult = true;  
  107.     }  
  108.     catch (Exception ex)  
  109.     {  
  110. bResult = false;  
  111. throw ex;  
  112.     }  
  113. }  //C#启动Windows服务及关闭
  114.     }  
  115.     return bResult;  
C#启动Windows服务及关闭具体实现就向你介绍到这里,希望对你了解和学习C#启动Windows服务及关闭有所帮助。

【编辑推荐】

  1. C#Windows服务程序安装浅析
  2. C#Windows服务程序开发的体会
  3. C#启动windows服务的方法浅析
  4. C#windows服务状态改变操作浅析
  5. C#Windows服务程序开发实例介绍
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-14 17:04:19

Windows后台服务

2009-08-14 16:02:50

C#启动windows

2009-08-24 10:37:27

C# 泛型

2009-08-14 16:41:22

C#启动Windows

2009-03-27 10:10:13

c#远程启动远程管理

2009-09-07 05:50:59

C# Timer用法

2009-09-01 18:29:10

C#继承C#多态

2009-08-31 17:16:12

C#实现接口

2009-08-21 10:13:02

C#异步初步

2009-08-26 11:32:37

C#打印文档

2009-08-26 11:07:36

C#打印窗体

2009-08-26 09:22:44

C#实现打印功能

2009-08-27 13:05:06

C#接口特点C#接口实例

2009-08-31 16:37:20

C#接口定义

2009-08-07 16:43:44

C#调用Windows

2009-08-25 15:38:12

C# Windows服

2009-08-17 09:39:40

C# Windows

2009-09-02 17:28:26

C#程序设计Windows窗体

2009-12-24 09:16:11

C#泛型

2009-08-27 18:09:49

C#接口的实现
点赞
收藏

51CTO技术栈公众号