⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#150例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.ServiceProcess;
using System.Diagnostics;


namespace Example119_分类枚举指定计算机的服务
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.ListBox listBox3;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.listBox2 = new System.Windows.Forms.ListBox();
			this.listBox3 = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "计算机:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(88, 8);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(72, 21);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(192, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "加载服务";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(24, 80);
			this.label2.TabIndex = 3;
			this.label2.Text = "启动的服务:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 152);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(24, 80);
			this.label3.TabIndex = 4;
			this.label3.Text = "暂停的服务:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 256);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(16, 88);
			this.label4.TabIndex = 5;
			this.label4.Text = "停止的服务:";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(72, 56);
			this.listBox1.Name = "listBox1";
			this.listBox1.ScrollAlwaysVisible = true;
			this.listBox1.Size = new System.Drawing.Size(192, 88);
			this.listBox1.TabIndex = 6;
			// 
			// listBox2
			// 
			this.listBox2.ItemHeight = 12;
			this.listBox2.Location = new System.Drawing.Point(72, 144);
			this.listBox2.Name = "listBox2";
			this.listBox2.ScrollAlwaysVisible = true;
			this.listBox2.Size = new System.Drawing.Size(192, 112);
			this.listBox2.TabIndex = 7;
			// 
			// listBox3
			// 
			this.listBox3.ItemHeight = 12;
			this.listBox3.Location = new System.Drawing.Point(72, 256);
			this.listBox3.Name = "listBox3";
			this.listBox3.ScrollAlwaysVisible = true;
			this.listBox3.Size = new System.Drawing.Size(192, 88);
			this.listBox3.TabIndex = 8;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 365);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listBox3,
																		  this.listBox2,
																		  this.listBox1,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.button1,
																		  this.textBox1,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string TempMachineName;
			TempMachineName =this.textBox1.Text;
			if (TempMachineName == "")
			{
				TempMachineName = System.Environment.MachineName;
				//如果textBox1中为空,采用本地计算机	
				this.textBox1.Focus();
				this.textBox1.Text = TempMachineName;
			}

			this.listBox1.Items.Clear();
			this.listBox2.Items.Clear();
			this.listBox3.Items.Clear();
            
			ServiceController[] ArraySrvCtrl; 
			ArraySrvCtrl = ServiceController.GetServices(TempMachineName);

			try
			{
				//在ArraySrvCtrl数组中存储所有服务
				foreach (ServiceController tempSC in ArraySrvCtrl)
				{
					if (tempSC.Status == ServiceControllerStatus.Running)
					{
						listBox1.Items.Add(tempSC.DisplayName);
						//将正在运行的服务添加到listBox1中
					}
					else if (tempSC.Status == ServiceControllerStatus.Paused)
					{ 	 
						listBox3.Items.Add(tempSC.DisplayName);
						//'将暂停的服务添加到listBox3中
					}
					else
					{
						listBox2.Items.Add(tempSC.DisplayName);
						//'将停止的服务添加到listBox2中
					}
				}
			}
			catch {}
		}	

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -