ServiceControlForm.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:11k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.ServiceProcess;
- namespace ServiceControl
- {
- /// <summary>
- /// Summary description for Form1.
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.ListBox listBoxServices;
- private System.Windows.Forms.Button buttonStart;
- private System.Windows.Forms.Button buttonStop;
- private System.Windows.Forms.Button buttonPause;
- private System.Windows.Forms.Button buttonContinue;
- private System.Windows.Forms.Button buttonRefresh;
- private System.Windows.Forms.Button buttonExit;
- private System.Windows.Forms.TextBox textDisplayName;
- private System.Windows.Forms.TextBox textServiceStatus;
- private System.Windows.Forms.TextBox textServiceType;
- private System.Windows.Forms.TextBox textServiceName;
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.Container components = null;
- private System.ServiceProcess.ServiceController[] services;
- public Form1()
- {
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent();
- RefreshServiceList();
- }
- protected void RefreshServiceList()
- {
- services = ServiceController.GetServices();
- listBoxServices.DisplayMember = "DisplayName";
- listBoxServices.DataSource = services;
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.textDisplayName = new System.Windows.Forms.TextBox();
- this.listBoxServices = new System.Windows.Forms.ListBox();
- this.buttonStart = new System.Windows.Forms.Button();
- this.textServiceStatus = new System.Windows.Forms.TextBox();
- this.textServiceType = new System.Windows.Forms.TextBox();
- this.textServiceName = new System.Windows.Forms.TextBox();
- this.buttonStop = new System.Windows.Forms.Button();
- this.buttonPause = new System.Windows.Forms.Button();
- this.buttonContinue = new System.Windows.Forms.Button();
- this.buttonRefresh = new System.Windows.Forms.Button();
- this.buttonExit = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // textDisplayName
- //
- this.textDisplayName.Location = new System.Drawing.Point(232, 16);
- this.textDisplayName.Name = "textDisplayName";
- this.textDisplayName.Size = new System.Drawing.Size(192, 20);
- this.textDisplayName.TabIndex = 1;
- this.textDisplayName.Text = "textDisplayName";
- //
- // listBoxServices
- //
- this.listBoxServices.Location = new System.Drawing.Point(8, 16);
- this.listBoxServices.Name = "listBoxServices";
- this.listBoxServices.Size = new System.Drawing.Size(208, 199);
- this.listBoxServices.TabIndex = 0;
- this.listBoxServices.SelectedIndexChanged += new System.EventHandler(this.OnSelectedIndexChanged);
- //
- // buttonStart
- //
- this.buttonStart.Location = new System.Drawing.Point(232, 120);
- this.buttonStart.Name = "buttonStart";
- this.buttonStart.Size = new System.Drawing.Size(88, 23);
- this.buttonStart.TabIndex = 5;
- this.buttonStart.Text = "Start";
- this.buttonStart.Click += new System.EventHandler(this.buttonCommand_Click);
- //
- // textServiceStatus
- //
- this.textServiceStatus.Location = new System.Drawing.Point(232, 40);
- this.textServiceStatus.Name = "textServiceStatus";
- this.textServiceStatus.Size = new System.Drawing.Size(192, 20);
- this.textServiceStatus.TabIndex = 2;
- this.textServiceStatus.Text = "textServiceStatus";
- //
- // textServiceType
- //
- this.textServiceType.Location = new System.Drawing.Point(232, 64);
- this.textServiceType.Name = "textServiceType";
- this.textServiceType.Size = new System.Drawing.Size(192, 20);
- this.textServiceType.TabIndex = 3;
- this.textServiceType.Text = "textServiceType";
- //
- // textServiceName
- //
- this.textServiceName.Location = new System.Drawing.Point(232, 88);
- this.textServiceName.Name = "textServiceName";
- this.textServiceName.Size = new System.Drawing.Size(192, 20);
- this.textServiceName.TabIndex = 4;
- this.textServiceName.Text = "textServiceName";
- //
- // buttonStop
- //
- this.buttonStop.Location = new System.Drawing.Point(336, 120);
- this.buttonStop.Name = "buttonStop";
- this.buttonStop.Size = new System.Drawing.Size(88, 23);
- this.buttonStop.TabIndex = 6;
- this.buttonStop.Text = "Stop";
- this.buttonStop.Click += new System.EventHandler(this.buttonCommand_Click);
- //
- // buttonPause
- //
- this.buttonPause.Location = new System.Drawing.Point(232, 152);
- this.buttonPause.Name = "buttonPause";
- this.buttonPause.Size = new System.Drawing.Size(88, 23);
- this.buttonPause.TabIndex = 7;
- this.buttonPause.Text = "Pause";
- this.buttonPause.Click += new System.EventHandler(this.buttonCommand_Click);
- //
- // buttonContinue
- //
- this.buttonContinue.Location = new System.Drawing.Point(336, 152);
- this.buttonContinue.Name = "buttonContinue";
- this.buttonContinue.Size = new System.Drawing.Size(88, 23);
- this.buttonContinue.TabIndex = 8;
- this.buttonContinue.Text = "Continue";
- this.buttonContinue.Click += new System.EventHandler(this.buttonCommand_Click);
- //
- // buttonRefresh
- //
- this.buttonRefresh.Location = new System.Drawing.Point(232, 200);
- this.buttonRefresh.Name = "buttonRefresh";
- this.buttonRefresh.Size = new System.Drawing.Size(88, 23);
- this.buttonRefresh.TabIndex = 9;
- this.buttonRefresh.Text = "Refresh";
- //
- // buttonExit
- //
- this.buttonExit.Location = new System.Drawing.Point(336, 200);
- this.buttonExit.Name = "buttonExit";
- this.buttonExit.Size = new System.Drawing.Size(88, 23);
- this.buttonExit.TabIndex = 10;
- this.buttonExit.Text = "Exit";
- this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(440, 237);
- this.Controls.Add(this.buttonExit);
- this.Controls.Add(this.buttonRefresh);
- this.Controls.Add(this.buttonContinue);
- this.Controls.Add(this.buttonPause);
- this.Controls.Add(this.buttonStop);
- this.Controls.Add(this.textServiceName);
- this.Controls.Add(this.textServiceType);
- this.Controls.Add(this.textServiceStatus);
- this.Controls.Add(this.buttonStart);
- this.Controls.Add(this.listBoxServices);
- this.Controls.Add(this.textDisplayName);
- this.Name = "Form1";
- this.Text = "Wrox Service Control";
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- protected string GetServiceTypeName(ServiceType type)
- {
- string serviceType = "";
- if ((type & ServiceType.InteractiveProcess) != 0)
- {
- serviceType = "Interactive ";
- type -= ServiceType.InteractiveProcess;
- }
- switch (type)
- {
- case ServiceType.Adapter:
- serviceType += "Adapter";
- break;
- case ServiceType.FileSystemDriver:
- case ServiceType.KernelDriver:
- case ServiceType.RecognizerDriver:
- serviceType += "Driver";
- break;
- case ServiceType.Win32OwnProcess:
- serviceType += "Win32 Service Process";
- break;
- case ServiceType.Win32ShareProcess:
- serviceType += "Win32 Shared Process";
- break;
- default:
- serviceType += "unknown type " + type.ToString();
- break;
- }
- return serviceType;
- }
- protected void SetServiceStatus(ServiceController controller)
- {
- buttonStart.Enabled = true;
- buttonStop.Enabled = true;
- buttonPause.Enabled = true;
- buttonContinue.Enabled = true;
- if (!controller.CanPauseAndContinue)
- {
- buttonPause.Enabled = false;
- buttonContinue.Enabled = false;
- }
- if (!controller.CanStop)
- {
- buttonStop.Enabled = false;
- }
- ServiceControllerStatus status = controller.Status;
- switch (status)
- {
- case ServiceControllerStatus.ContinuePending:
- textServiceStatus.Text = "Continue Pending";
- buttonContinue.Enabled = false;
- break;
- case ServiceControllerStatus.Paused:
- textServiceStatus.Text = "Paused";
- buttonPause.Enabled = false;
- buttonStart.Enabled = false;
- break;
- case ServiceControllerStatus.PausePending:
- textServiceStatus.Text = "Pause Pending";
- buttonPause.Enabled = false;
- buttonStart.Enabled = false;
- break;
- case ServiceControllerStatus.StartPending:
- textServiceStatus.Text = "Start Pending";
- buttonStart.Enabled = false;
- break;
- case ServiceControllerStatus.Running:
- textServiceStatus.Text = "Running";
- buttonStart.Enabled = false;
- buttonContinue.Enabled = false;
- break;
- case ServiceControllerStatus.Stopped:
- textServiceStatus.Text = "Stopped";
- buttonStop.Enabled = false;
- break;
- case ServiceControllerStatus.StopPending:
- textServiceStatus.Text = "Stop Pending";
- buttonStop.Enabled = false;
- break;
- default:
- textServiceStatus.Text = "Unknown status";
- break;
- }
- }
-
- protected void OnSelectedIndexChanged (object sender,
- System.EventArgs e)
- {
- ServiceController controller =
- (ServiceController)listBoxServices.SelectedItem;
- textDisplayName.Text = controller.DisplayName;
- textServiceType.Text =
- GetServiceTypeName(controller.ServiceType);
- textServiceName.Text = controller.ServiceName;
- SetServiceStatus(controller);
- }
- protected void buttonCommand_Click(object sender, System.EventArgs e)
- {
- Cursor.Current = Cursors.WaitCursor;
- ServiceController controller =
- (ServiceController)listBoxServices.SelectedItem;
- if (sender == this.buttonStart)
- {
- controller.Start();
- controller.WaitForStatus(ServiceControllerStatus.Running);
- }
- else if (sender == this.buttonStop)
- {
- controller.Stop();
- controller.WaitForStatus(ServiceControllerStatus.Stopped);
- }
- else if (sender == this.buttonPause)
- {
- controller.Pause();
- controller.WaitForStatus(ServiceControllerStatus.Paused);
- }
- else if (sender == this.buttonContinue)
- {
- controller.Continue();
- controller.WaitForStatus(ServiceControllerStatus.Running);
- }
- int index =listBoxServices.SelectedIndex;
- RefreshServiceList();
- listBoxServices.SelectedIndex = index;
- Cursor.Current = Cursors.Default;
- }
- protected void buttonExit_Click(object sender, System.EventArgs e)
- {
- Application.Exit();
- }
- protected void buttonRefresh_Click(object sender, System.EventArgs e)
- {
- RefreshServiceList();
- }
- }
- }