ServiceControlForm.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:11k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  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. namespace ServiceControl
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.ListBox listBoxServices;
  16. private System.Windows.Forms.Button buttonStart;
  17. private System.Windows.Forms.Button buttonStop;
  18. private System.Windows.Forms.Button buttonPause;
  19. private System.Windows.Forms.Button buttonContinue;
  20. private System.Windows.Forms.Button buttonRefresh;
  21. private System.Windows.Forms.Button buttonExit;
  22. private System.Windows.Forms.TextBox textDisplayName;
  23. private System.Windows.Forms.TextBox textServiceStatus;
  24. private System.Windows.Forms.TextBox textServiceType;
  25. private System.Windows.Forms.TextBox textServiceName;
  26. /// <summary>
  27. /// Required designer variable.
  28. /// </summary>
  29. private System.ComponentModel.Container components = null;
  30. private System.ServiceProcess.ServiceController[] services;
  31. public Form1()
  32. {
  33. //
  34. // Required for Windows Form Designer support
  35. //
  36. InitializeComponent();
  37. RefreshServiceList();
  38. }
  39. protected void RefreshServiceList()
  40. {
  41. services = ServiceController.GetServices();
  42. listBoxServices.DisplayMember = "DisplayName";
  43. listBoxServices.DataSource = services;
  44. }
  45. /// <summary>
  46. /// Clean up any resources being used.
  47. /// </summary>
  48. protected override void Dispose( bool disposing )
  49. {
  50. if( disposing )
  51. {
  52. if (components != null) 
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose( disposing );
  58. }
  59. #region Windows Form Designer generated code
  60. /// <summary>
  61. /// Required method for Designer support - do not modify
  62. /// the contents of this method with the code editor.
  63. /// </summary>
  64. private void InitializeComponent()
  65. {
  66. this.textDisplayName = new System.Windows.Forms.TextBox();
  67. this.listBoxServices = new System.Windows.Forms.ListBox();
  68. this.buttonStart = new System.Windows.Forms.Button();
  69. this.textServiceStatus = new System.Windows.Forms.TextBox();
  70. this.textServiceType = new System.Windows.Forms.TextBox();
  71. this.textServiceName = new System.Windows.Forms.TextBox();
  72. this.buttonStop = new System.Windows.Forms.Button();
  73. this.buttonPause = new System.Windows.Forms.Button();
  74. this.buttonContinue = new System.Windows.Forms.Button();
  75. this.buttonRefresh = new System.Windows.Forms.Button();
  76. this.buttonExit = new System.Windows.Forms.Button();
  77. this.SuspendLayout();
  78. // 
  79. // textDisplayName
  80. // 
  81. this.textDisplayName.Location = new System.Drawing.Point(232, 16);
  82. this.textDisplayName.Name = "textDisplayName";
  83. this.textDisplayName.Size = new System.Drawing.Size(192, 20);
  84. this.textDisplayName.TabIndex = 1;
  85. this.textDisplayName.Text = "textDisplayName";
  86. // 
  87. // listBoxServices
  88. // 
  89. this.listBoxServices.Location = new System.Drawing.Point(8, 16);
  90. this.listBoxServices.Name = "listBoxServices";
  91. this.listBoxServices.Size = new System.Drawing.Size(208, 199);
  92. this.listBoxServices.TabIndex = 0;
  93. this.listBoxServices.SelectedIndexChanged += new System.EventHandler(this.OnSelectedIndexChanged);
  94. // 
  95. // buttonStart
  96. // 
  97. this.buttonStart.Location = new System.Drawing.Point(232, 120);
  98. this.buttonStart.Name = "buttonStart";
  99. this.buttonStart.Size = new System.Drawing.Size(88, 23);
  100. this.buttonStart.TabIndex = 5;
  101. this.buttonStart.Text = "Start";
  102. this.buttonStart.Click += new System.EventHandler(this.buttonCommand_Click);
  103. // 
  104. // textServiceStatus
  105. // 
  106. this.textServiceStatus.Location = new System.Drawing.Point(232, 40);
  107. this.textServiceStatus.Name = "textServiceStatus";
  108. this.textServiceStatus.Size = new System.Drawing.Size(192, 20);
  109. this.textServiceStatus.TabIndex = 2;
  110. this.textServiceStatus.Text = "textServiceStatus";
  111. // 
  112. // textServiceType
  113. // 
  114. this.textServiceType.Location = new System.Drawing.Point(232, 64);
  115. this.textServiceType.Name = "textServiceType";
  116. this.textServiceType.Size = new System.Drawing.Size(192, 20);
  117. this.textServiceType.TabIndex = 3;
  118. this.textServiceType.Text = "textServiceType";
  119. // 
  120. // textServiceName
  121. // 
  122. this.textServiceName.Location = new System.Drawing.Point(232, 88);
  123. this.textServiceName.Name = "textServiceName";
  124. this.textServiceName.Size = new System.Drawing.Size(192, 20);
  125. this.textServiceName.TabIndex = 4;
  126. this.textServiceName.Text = "textServiceName";
  127. // 
  128. // buttonStop
  129. // 
  130. this.buttonStop.Location = new System.Drawing.Point(336, 120);
  131. this.buttonStop.Name = "buttonStop";
  132. this.buttonStop.Size = new System.Drawing.Size(88, 23);
  133. this.buttonStop.TabIndex = 6;
  134. this.buttonStop.Text = "Stop";
  135. this.buttonStop.Click += new System.EventHandler(this.buttonCommand_Click);
  136. // 
  137. // buttonPause
  138. // 
  139. this.buttonPause.Location = new System.Drawing.Point(232, 152);
  140. this.buttonPause.Name = "buttonPause";
  141. this.buttonPause.Size = new System.Drawing.Size(88, 23);
  142. this.buttonPause.TabIndex = 7;
  143. this.buttonPause.Text = "Pause";
  144. this.buttonPause.Click += new System.EventHandler(this.buttonCommand_Click);
  145. // 
  146. // buttonContinue
  147. // 
  148. this.buttonContinue.Location = new System.Drawing.Point(336, 152);
  149. this.buttonContinue.Name = "buttonContinue";
  150. this.buttonContinue.Size = new System.Drawing.Size(88, 23);
  151. this.buttonContinue.TabIndex = 8;
  152. this.buttonContinue.Text = "Continue";
  153. this.buttonContinue.Click += new System.EventHandler(this.buttonCommand_Click);
  154. // 
  155. // buttonRefresh
  156. // 
  157. this.buttonRefresh.Location = new System.Drawing.Point(232, 200);
  158. this.buttonRefresh.Name = "buttonRefresh";
  159. this.buttonRefresh.Size = new System.Drawing.Size(88, 23);
  160. this.buttonRefresh.TabIndex = 9;
  161. this.buttonRefresh.Text = "Refresh";
  162. // 
  163. // buttonExit
  164. // 
  165. this.buttonExit.Location = new System.Drawing.Point(336, 200);
  166. this.buttonExit.Name = "buttonExit";
  167. this.buttonExit.Size = new System.Drawing.Size(88, 23);
  168. this.buttonExit.TabIndex = 10;
  169. this.buttonExit.Text = "Exit";
  170. this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
  171. // 
  172. // Form1
  173. // 
  174. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  175. this.ClientSize = new System.Drawing.Size(440, 237);
  176. this.Controls.Add(this.buttonExit);
  177. this.Controls.Add(this.buttonRefresh);
  178. this.Controls.Add(this.buttonContinue);
  179. this.Controls.Add(this.buttonPause);
  180. this.Controls.Add(this.buttonStop);
  181. this.Controls.Add(this.textServiceName);
  182. this.Controls.Add(this.textServiceType);
  183. this.Controls.Add(this.textServiceStatus);
  184. this.Controls.Add(this.buttonStart);
  185. this.Controls.Add(this.listBoxServices);
  186. this.Controls.Add(this.textDisplayName);
  187. this.Name = "Form1";
  188. this.Text = "Wrox Service Control";
  189. this.ResumeLayout(false);
  190. }
  191. #endregion
  192. /// <summary>
  193. /// The main entry point for the application.
  194. /// </summary>
  195. [STAThread]
  196. static void Main() 
  197. {
  198. Application.Run(new Form1());
  199. }
  200. protected string GetServiceTypeName(ServiceType type)
  201. {
  202. string serviceType = "";
  203. if ((type & ServiceType.InteractiveProcess) != 0)
  204. {
  205. serviceType = "Interactive ";
  206. type -= ServiceType.InteractiveProcess;
  207. }
  208. switch (type)
  209. {
  210. case ServiceType.Adapter:
  211. serviceType += "Adapter";
  212. break;
  213. case ServiceType.FileSystemDriver:
  214. case ServiceType.KernelDriver:
  215. case ServiceType.RecognizerDriver:
  216. serviceType += "Driver";
  217. break;
  218. case ServiceType.Win32OwnProcess:
  219. serviceType += "Win32 Service Process";
  220. break;
  221. case ServiceType.Win32ShareProcess:
  222. serviceType += "Win32 Shared Process";
  223. break;
  224. default:
  225. serviceType += "unknown type " + type.ToString();
  226. break;
  227. }
  228. return serviceType;
  229. }
  230. protected void SetServiceStatus(ServiceController controller) 
  231. {
  232. buttonStart.Enabled = true;
  233. buttonStop.Enabled = true;
  234. buttonPause.Enabled = true;
  235. buttonContinue.Enabled = true;
  236. if (!controller.CanPauseAndContinue)
  237. {
  238. buttonPause.Enabled = false;
  239. buttonContinue.Enabled = false;
  240. }
  241. if (!controller.CanStop)
  242. {
  243. buttonStop.Enabled = false;
  244. }
  245. ServiceControllerStatus status = controller.Status;
  246. switch (status)
  247. {
  248. case ServiceControllerStatus.ContinuePending:
  249. textServiceStatus.Text = "Continue Pending";
  250. buttonContinue.Enabled = false;
  251. break;
  252. case ServiceControllerStatus.Paused:
  253. textServiceStatus.Text = "Paused";
  254. buttonPause.Enabled = false; 
  255. buttonStart.Enabled = false;
  256. break;
  257. case ServiceControllerStatus.PausePending:
  258. textServiceStatus.Text = "Pause Pending";
  259. buttonPause.Enabled = false; 
  260. buttonStart.Enabled = false;
  261. break;
  262. case ServiceControllerStatus.StartPending:
  263. textServiceStatus.Text = "Start Pending";
  264. buttonStart.Enabled = false;
  265. break;
  266. case ServiceControllerStatus.Running:
  267. textServiceStatus.Text = "Running";
  268. buttonStart.Enabled = false; 
  269. buttonContinue.Enabled = false;
  270. break;
  271. case ServiceControllerStatus.Stopped:
  272. textServiceStatus.Text = "Stopped";
  273. buttonStop.Enabled = false;
  274. break;
  275. case ServiceControllerStatus.StopPending:
  276. textServiceStatus.Text = "Stop Pending";
  277. buttonStop.Enabled = false;
  278. break;
  279. default:
  280. textServiceStatus.Text = "Unknown status";
  281. break;
  282. }
  283. }
  284.                       
  285. protected void OnSelectedIndexChanged (object sender, 
  286. System.EventArgs e)
  287. {
  288. ServiceController controller = 
  289. (ServiceController)listBoxServices.SelectedItem;
  290. textDisplayName.Text = controller.DisplayName;
  291. textServiceType.Text = 
  292. GetServiceTypeName(controller.ServiceType);
  293. textServiceName.Text = controller.ServiceName;
  294. SetServiceStatus(controller);
  295. }
  296. protected void buttonCommand_Click(object sender, System.EventArgs e) 
  297. {
  298. Cursor.Current = Cursors.WaitCursor;
  299. ServiceController controller = 
  300. (ServiceController)listBoxServices.SelectedItem;
  301. if (sender == this.buttonStart)
  302. {
  303. controller.Start();
  304. controller.WaitForStatus(ServiceControllerStatus.Running);
  305. }
  306. else if (sender == this.buttonStop)
  307. {
  308. controller.Stop();
  309. controller.WaitForStatus(ServiceControllerStatus.Stopped);
  310. }
  311. else if (sender == this.buttonPause)
  312. {
  313. controller.Pause();
  314. controller.WaitForStatus(ServiceControllerStatus.Paused);
  315. }
  316. else if (sender == this.buttonContinue)
  317. {
  318. controller.Continue();
  319. controller.WaitForStatus(ServiceControllerStatus.Running);
  320. }
  321. int index =listBoxServices.SelectedIndex;
  322. RefreshServiceList();
  323. listBoxServices.SelectedIndex = index;
  324. Cursor.Current = Cursors.Default;
  325. }
  326. protected void buttonExit_Click(object sender, System.EventArgs e)
  327. {
  328. Application.Exit();
  329. }
  330. protected void buttonRefresh_Click(object sender, System.EventArgs e)
  331. {
  332. RefreshServiceList();
  333. }
  334. }
  335. }