C#启动Windows服务的窗体程序浅析

开发 后端
C#启动Windows服务的窗体程序主要向你介绍了C#随机启动、监控WINDOWS服务的窗体程序,那么C#启动Windows服务的窗体程序的思路和代码实现如何呢?那么本文就向你一一介绍。

C#启动Windows服务的窗体程序的由来:最近应客户的要求,做了一个定时监控WINDOWS服务的程序,要做到随机启动,定时监控指定的服务,如果没有开启则开启,由于时间仓促,没有经过长时间的服务器端运行,现将思路及代码公布,以后有改进会及时更新:

一、C#启动Windows服务的窗体程序思路:

本程序的核心在于随机启动和WINDOWS服务上,对于随机启动,引入Microsoft.Win32命名空间,利用RegistryKey类即可完成对注册表的增、删、改等操作;对于WINDOWS服务,引入System.ServiceProcess命名空间,利用ServiceController类即可完成对系统服务的启动、停止、查询等操作。改日就测试程序的稳定性及资源消耗率等指标。

二、C#启动Windows服务的窗体程序代码如下,这里程序默认为开启MSSQLSERVER服务,并添加了托盘区图标,可以在启动时或启动后最小化到托盘区:

  1. using System;  //C#启动Windows服务的窗体程序
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.windows.Forms;  
  6. using System.Data;  
  7. using System.ServiceProcess;  
  8. using System.IO;  
  9. using Microsoft.Win32;  
  10. namespace WatchService  
  11. {  
  12. /// ﹤summary﹥  
  13. /// Form1 的摘要说明,C#启动Windows服务的窗体程序  
  14. /// ﹤/summary﹥  
  15. public class WatchService : System.windows.Forms.Form  
  16. {  
  17. private System.windows.Forms.Button btn_startWatch;  
  18. private System.windows.Forms.Button btn_stopWatch;  
  19. private System.windows.Forms.Button btn_startReg;  
  20. private System.windows.Forms.Button btn_stopReg;  
  21. private System.windows.Forms.Label lbl_appStatus;  
  22. private System.windows.Forms.TextBox tbx_serviceName;  
  23. private System.windows.Forms.Button btn_Exit;  
  24. private System.windows.Forms.TextBox tbx_interval;  
  25. private System.windows.Forms.Timer timer1;  
  26. private System.windows.Forms.NotifyIcon notifyIcon1;  
  27. private System.ComponentModel.IContainer components;  
  28. public WatchService()  
  29. {  
  30. // C#启动Windows服务的窗体程序 
  31. // windows 窗体设计器支持所必需的  
  32. //  
  33. InitializeComponent();  
  34. //  
  35. // TODO: 在 InitializeComponent   
  36. //调用后添加任何构造函数代码  
  37. //  
  38. }  
  39. /// ﹤summary﹥
  40. ///C#启动Windows服务的窗体程序  
  41. /// 清理所有正在使用的资源。  
  42. /// ﹤/summary﹥  
  43. protected override void Dispose( bool disposing )  
  44. {  
  45. if( disposing )  
  46. {  
  47. if (components != null)   
  48. {  
  49. components.Dispose();  
  50. }  
  51. }  
  52. base.Dispose( disposing );  
  53. }  
  54. #region windows 窗体设计器生成的代码  
  55. /// ﹤summary﹥  
  56. /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  57. /// 此方法的内容。  
  58. /// ﹤/summary﹥  
  59. private void InitializeComponent()  
  60. {  
  61. this.components = new System.ComponentModel.Container();  
  62. System.Resources.ResourceManager resources = new 
  63.  System.Resources.ResourceManager(typeof(WatchService));  
  64. this.btn_startWatch = new System.windows.Forms.Button();  
  65. this.btn_stopWatch = new System.windows.Forms.Button();  
  66. this.btn_startReg = new System.windows.Forms.Button();  
  67. this.btn_stopReg = new System.windows.Forms.Button();  
  68. this.lbl_appStatus = new System.windows.Forms.Label();  
  69. this.tbx_serviceName = new System.windows.Forms.TextBox();  
  70. this.btn_Exit = new System.windows.Forms.Button();  
  71. this.tbx_interval = new System.windows.Forms.TextBox();  
  72. this.timer1 = new System.windows.Forms.Timer(this.components);  
  73. this.notifyIcon1 = new   
  74. System.windows.Forms.NotifyIcon(this.components);  
  75. this.SuspendLayout();  
  76. //   
  77. // btn_startWatch  
  78. //   C#启动Windows服务的窗体程序
  79. this.btn_startWatch.Location = new System.Drawing.Point(112, 8);  
  80. this.btn_startWatch.Name = "btn_startWatch";  
  81. this.btn_startWatch.Size = new System.Drawing.Size(64, 23);  
  82. this.btn_startWatch.TabIndex = 0;  
  83. this.btn_startWatch.Text = "开始监控";  
  84. this.btn_startWatch.Click +=   
  85. new System.EventHandler(this.btn_startWatch_Click);  
  86. //   
  87. // btn_stopWatch  
  88. //   
  89. this.btn_stopWatch.Location = new System.Drawing.Point(184, 8);  
  90. this.btn_stopWatch.Name = "btn_stopWatch";  
  91. this.btn_stopWatch.Size = new System.Drawing.Size(64, 23);  
  92. this.btn_stopWatch.TabIndex = 1;  
  93. this.btn_stopWatch.Text = "停止监控";  
  94. //   
  95. // btn_startReg  
  96. //   C#启动Windows服务的窗体程序
  97. this.btn_startReg.Location = new System.Drawing.Point(112, 40);  
  98. this.btn_startReg.Name = "btn_startReg";  
  99. this.btn_startReg.Size = new System.Drawing.Size(88, 24);  
  100. this.btn_startReg.TabIndex = 2;  
  101. this.btn_startReg.Text = "开启随机启动";  
  102. this.btn_startReg.Click += new   
  103. System.EventHandler(this.btn_startReg_Click);  
  104. //   
  105. // btn_stopReg  
  106. //   
  107. this.btn_stopReg.Location = new System.Drawing.Point(232, 40);  
  108. this.btn_stopReg.Name = "btn_stopReg";  
  109. this.btn_stopReg.Size = new System.Drawing.Size(88, 24);  
  110. this.btn_stopReg.TabIndex = 3;  
  111. this.btn_stopReg.Text = "关闭随机启动";  
  112. this.btn_stopReg.Click += new   
  113. System.EventHandler(this.btn_stopReg_Click);  
  114. //   
  115. // lbl_appStatus  
  116. //   C#启动Windows服务的窗体程序
  117. this.lbl_appStatus.ForeColor = System.Drawing.Color.Red;  
  118. this.lbl_appStatus.Location = new System.Drawing.Point(16, 72);  
  119. this.lbl_appStatus.Name = "lbl_appStatus";  
  120. this.lbl_appStatus.Size = new System.Drawing.Size(304, 23);  
  121. this.lbl_appStatus.TabIndex = 4;  
  122. this.lbl_appStatus.TextAlign =   
  123. System.Drawing.ContentAlignment.MiddleLeft;  
  124. //   
  125. // tbx_serviceName  
  126. //   
  127. this.tbx_serviceName.BorderStyle =   
  128. System.windows.Forms.BorderStyle.FixedSingle;  
  129. this.tbx_serviceName.ForeColor = System.Drawing.Color.Red;  
  130. this.tbx_serviceName.Location = new System.Drawing.Point(8, 8);  
  131. this.tbx_serviceName.Name = "tbx_serviceName";  
  132. this.tbx_serviceName.TabIndex = 5;  
  133. this.tbx_serviceName.Text = "输入服务名";  
  134. this.tbx_serviceName.MouseDown += new 
  135.  System.windows.Forms.MouseEventHandler(  
  136. this.tbx_serviceName_MouseDown);  
  137. //   
  138. // btn_Exit  
  139. //   
  140. this.btn_Exit.Location = new System.Drawing.Point(256, 8);  
  141. this.btn_Exit.Name = "btn_Exit";  
  142. this.btn_Exit.Size = new System.Drawing.Size(64, 23);  
  143. this.btn_Exit.TabIndex = 6;  
  144. this.btn_Exit.Text = "退出程序";  
  145. this.btn_Exit.Click += new System.EventHandler(this.btn_Exit_Click);  
  146. //   
  147. // tbx_interval  
  148. //   C#启动Windows服务的窗体程序
  149. this.tbx_interval.BorderStyle =  
  150.  System.windows.Forms.BorderStyle.FixedSingle;  
  151. this.tbx_interval.ForeColor = System.Drawing.Color.Red;  
  152. this.tbx_interval.Location = new System.  
  153. Drawing.Point(8, 40);  
  154. this.tbx_interval.Name = "tbx_interval";  
  155. this.tbx_interval.TabIndex = 7;  
  156. this.tbx_interval.Text = "输入监控间隔(秒)";  
  157. this.tbx_interval.MouseDown += new 
  158.  System.windows.Forms.MouseEventHandler(  
  159. this.tbx_interval_MouseDown);  
  160. //   
  161. // timer1  
  162. //   
  163. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  
  164. //   
  165. // notifyIcon1  
  166. //   
  167. this.notifyIcon1.Icon = ((System.Drawing.Icon)  
  168. (resources.GetObject("notifyIcon1.Icon")));  
  169. this.notifyIcon1.Text = "双击打开WatchService";  
  170. this.notifyIcon1.Visible = true;  
  171. this.notifyIcon1.DoubleClick +=   
  172. new System.EventHandler(this.notifyIcon1_DoubleClick);  
  173. //   
  174. // WatchService  
  175. //   C#启动Windows服务的窗体程序
  176. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  177. this.ClientSize = new System.Drawing.Size(328, 102);  
  178. this.Controls.Add(this.tbx_interval);  
  179. this.Controls.Add(this.btn_Exit);  
  180. this.Controls.Add(this.tbx_serviceName);  
  181. this.Controls.Add(this.lbl_appStatus);  
  182. this.Controls.Add(this.btn_stopReg);  
  183. this.Controls.Add(this.btn_startReg);  
  184. this.Controls.Add(this.btn_stopWatch);  
  185. this.Controls.Add(this.btn_startWatch);  
  186. this.MaximizeBox = false;  
  187. this.Name = "WatchService";  
  188. this.ShowInTaskbar = false;  
  189. this.Text = "WatchService";  
  190. this.windowstate = System.windows.  
  191. Forms.Formwindowstate.Minimized;  
  192. this.Resize += new System.EventHandler(  
  193. this.WatchService_Resize);  
  194. this.Load += new System.EventHandler(  
  195. this.WatchService_Load);  
  196. this.ResumeLayout(false);  
  197. }  
  198. #endregion  
  199. private ServiceController scDBService;  
  200. private string serviceName="MSSqlServer";   
  201. // 默认服务名  
  202. private int iInterval=60;  
  203.  // 默认监控间隔(秒)  
  204. /// ﹤summary﹥  
  205. ///C#启动Windows服务的窗体程序
  206. /// 应用程序的主入口点。  
  207. /// ﹤/summary﹥  
  208. [STAThread]  
  209. static void Main()   
  210. {  
  211. Application.Run(new WatchService());  
  212. }  
  213. // 开启监控  
  214. private void btn_startWatch_Click(  
  215. object sender, System.EventArgs e)  
  216. {  
  217. if(this.tbx_serviceName.Text=="")  
  218. {  
  219. this.lbl_appStatus.Text="服务名不能为空";  
  220. this.tbx_serviceName.Focus();  
  221. return;  
  222. }  
  223. if(this.tbx_interval.Text=="")  
  224. {  
  225. this.lbl_appStatus.Text="服务监控间隔不能为空";  
  226. this.tbx_interval.Focus();  
  227. return;  
  228. }  
  229. serviceName=this.tbx_serviceName.Text;  
  230. iInterval=int.Parse(this.tbx_interval.Text);  
  231. this.timer1.Interval=iInterval*1000;  
  232. startService();  
  233. }  
  234. // 开启随机启动  
  235. private void btn_startReg_Click(  
  236. object sender, System.EventArgs e)  
  237. {  
  238. try 
  239. {  
  240. string dir=Directory.GetCurrentDirectory();  
  241. dir+="\\WatchService.exe";  
  242. RegistryKey akey=Registry.LocalMachine;  
  243. akey=akey.OpenSubKey(@"SOFTWARE\Microsoft  
  244. \windows\CurrentVersion\Run",true);  
  245. akey.SetValue("WatchService",dir);  
  246. akey.Close();  
  247. this.lbl_appStatus.Text="开启随机启动成功。";  
  248. }  
  249. catch(Exception exp)  
  250. {  
  251. this.lbl_appStatus.Text="开启随机启动失败,  
  252. 原因:"+exp.Message;  
  253. }  
  254. }  //C#启动Windows服务的窗体程序
  255. private void tbx_serviceName_MouseDown(  
  256. object sender, System.windows.Forms.MouseEventArgs e)  
  257. {  
  258. this.tbx_serviceName.Text="";  
  259. }  
  260. // 关闭随机启动  
  261. private void btn_stopReg_Click(object sender,  
  262.  System.EventArgs e)  
  263. {  
  264. try 
  265. {  
  266. RegistryKey akey=Registry.LocalMachine;  
  267. akey=akey.OpenSubKey(@"SOFTWARE\Microsoft  
  268. \windows\CurrentVersion\Run",true);  
  269. akey.SetValue("WatchService",false);  
  270. akey.Close();  
  271. this.lbl_appStatus.Text="关闭随机启动成功。";  
  272. }  
  273. catch(Exception exp)  
  274. {  
  275. this.lbl_appStatus.Text="关闭随机启动失败,原因:"+exp.Message;  
  276. }  
  277. }  
  278. private void btn_Exit_Click(object sender, System.EventArgs e)  
  279. {  
  280. Application.Exit();  
  281. }  
  282. /// ﹤summary﹥  
  283. /// 开启指定的windows服务  
  284. ///C#启动Windows服务的窗体程序
  285. /// ﹤/summary﹥  
  286. private void startService()  
  287. {  
  288. try 
  289. {  
  290. scDBService=new ServiceController(serviceName);  
  291. ServiceController[] scAllService=  
  292. ServiceController.GetServices();  
  293. int i=0;  
  294. while(i﹤scAllService.Length)  
  295. {  
  296. if(scAllService[i].DisplayName==serviceName)  
  297. {  
  298. if(scDBService.Status.Equals(ServiceControllerStatus.Stopped))  
  299. {  
  300. this.lbl_appStatus.Text=serviceName+"  
  301. 服务正在启动……";  
  302. scDBService.Start();  
  303. }  
  304. else if(scDBService.Status.Equals(  
  305. ServiceControllerStatus.Running))  
  306. {  
  307. this.lbl_appStatus.Text=serviceName+"  
  308. 服务正在运行……";  
  309. }  
  310. if(!this.timer1.Enabled) this.timer1.Start();  
  311. return;  
  312. }  
  313. else 
  314. {  
  315. i++;  
  316. }  
  317. }  
  318. this.lbl_appStatus.Text=serviceName+"  
  319. 服务并没有安装在本机上,请检查。";  
  320. if(this.timer1.Enabled) this.timer1.Stop();  
  321. }  
  322. catch(Exception exp)  
  323. {  
  324. this.lbl_appStatus.Text=serviceName+  
  325. "服务启动失败,原因:"+exp.Message;  
  326. }   
  327. }  
  328. // 监控时钟  
  329. private void timer1_Tick(object sender,   
  330. System.EventArgs e)  
  331. {  
  332. startService();   
  333. }  
  334. private void tbx_interval_MouseDown(object sender,  
  335.  System.windows.Forms.MouseEventArgs e)  
  336. {  
  337. this.tbx_interval.Text="";  
  338. }  
  339. // 窗体加载后即最小化到托盘区  
  340. private void WatchService_Load(  
  341. object sender, System.EventArgs e)  
  342. {  
  343. this.Hide();  
  344. this.notifyIcon1.Visible=true;  
  345. startService();  
  346. }  
  347. // 窗体最小化  
  348. private void WatchService_Resize(  
  349. object sender, System.EventArgs e)  
  350. {  
  351. if (this.windowstate==Formwindowstate.Minimized)  
  352. {  
  353. this.Hide();  
  354. this.notifyIcon1.Visible=true;  
  355. }  
  356. }  
  357. // 托盘区图标操作  
  358. private void notifyIcon1_DoubleClick(  
  359. object sender, System.EventArgs e)  
  360. {  
  361. this.Visible = true;  
  362. this.windowstate = Formwindowstate.Normal;  
  363. this.notifyIcon1.Visible = false;  
  364. }  
  365. // 停止监控  
  366. private void btn_stopWatch_Click(  
  367. object sender, System.EventArgs e)  
  368. {  
  369. this.timer1.Stop();  
  370. }  
  371. }  

C#启动Windows服务的窗体程序的基本情况就向你介绍到这里,希望对你了解和学习C#启动Windows服务的窗体程序有所帮助。

【编辑推荐】

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

2009-08-25 09:39:21

创建C# Window

2009-09-02 17:28:26

C#程序设计Windows窗体

2009-08-14 16:02:50

C#启动windows

2009-08-14 11:00:16

C#创建Windows

2009-09-02 17:53:42

C#程序设计Windows窗体

2009-08-20 10:10:55

C#透明窗体

2009-09-07 06:07:46

C#窗体设计

2009-09-07 04:19:56

C#窗体事件

2009-09-07 04:56:52

C#模式窗体

2009-09-07 05:31:39

C#窗体关闭事件

2009-09-07 03:37:51

C#窗体

2009-09-07 06:56:46

C#透明窗体

2009-09-07 05:24:22

C#窗体继承

2009-08-14 14:25:09

Windows服务程序

2009-08-14 15:47:18

C#Windows服务

2009-08-14 15:06:08

Windows服务程序

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 14:17:16

C#Windows服务

2009-08-14 17:04:19

Windows后台服务

2009-09-07 14:00:57

C#抓取网页
点赞
收藏

51CTO技术栈公众号