通过 WinForms 中的 ServiceController 控制已安装的服务





5.00/5 (3投票s)
了解如何在 Windows Forms 应用程序中通过 ServiceController 控制 Windows 服务。
引言
在这里,我将简单解释如何通过 Windows Forms 应用程序控制机器中已安装的服务。该应用程序将
- 列出并显示所有可用的服务
- 控制特定服务
- 显示详细的服务信息
背景
- 对 C# 有中级了解
- 您应该对 Windows Forms 有一些了解
要求
- Visual Studio
- .NET Framework
- Windows 机器
Using the Code
步骤 1 - 设计窗体
首先,您必须从预先安装的 C# 项目中创建一个新的Windows Forms 应用程序。
我将此项目命名为 "ControlServices
"。
单击工具箱,然后将 2 个按钮、2 个列表框、2 个文本框和 4 个标签拖放到窗体设计器中,如下面的图像所示
我建议您更改属性中的按钮名称。从工具箱控制器中,添加一个 ServiceController
项,然后将其命名为 ServiceController1
。在服务控制器属性中,选择 Appinfo
作为默认服务名称。
步骤 2 - 列出服务并显示服务名称
要在 listBox2
上显示所有已安装的服务,首先添加以下内容:双击“列出服务”按钮并添加以下代码
private void button2ListServices_Click(object sender, EventArgs e)
{
ServiceController[] AllServices; // creating an array of services
AllServices = ServiceController.GetServices();// getting all services
foreach (ServiceController oneService in AllServices)
{
listBox2.Items.Add(oneService.ServiceName.ToString());// inserting service names in
// listbox one by one
}
}
不要忘记添加 using System.ServiceProcess;
命名空间。
这是一个示例运行
您可能会注意到,某些服务看起来毫无意义,因此我们需要在用户单击 listbox
上的任何服务时显示完整的服务名称。为此,双击 listbox2
,并添加以下代码
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
serviceController1.ServiceName = listBox2.SelectedItem.ToString();
textBox2.Text = serviceController1.DisplayName.ToString();
}
例如:当用户单击 ehSched
服务时,应用程序会显示其全名Windows Media Center 计划程序服务。
步骤 3 - 控制服务、启动、停止和暂停它们
我们将按此顺序控制已安装的进程。
如果所选进程已停止,我们将尝试启动它;如果它正在运行,我们将尝试暂停它;如果它已暂停,我们将尝试停止它。
当程序无法启动、停止或暂停进程时,它将显示一个异常,说明无法完成该任务。
建议使用 using System.Threading;
命名空间,但不是强制性的。
要实现上述应用程序功能,请双击 listbox2
并添加以下代码
textBox1.BackColor = System.Drawing.Color.White;
textBox1.Text = serviceController1.Status.ToString();
然后返回到窗体设计器并双击控制服务按钮,并添加以下代码
private void button1ControlService_Click(object sender, EventArgs e)
{
if (this.serviceController1.Status.ToString() == "Stopped")
{
Thread.Sleep(2000);
try // trying to start a service
{
this.serviceController1.Start();
textBox1.Text = "Running";//serviceController1.Status.ToString();
textBox1.BackColor = System.Drawing.Color.Red;// change textbox color to red when
// process status is changed
}
catch (Exception) // exception thrown
{
//handling exception
MessageBox.Show("Could not start this process",
"Process failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.serviceController1.Close();
}
}
else if (serviceController1.Status.ToString() == "Running")
{
Thread.Sleep(1500);
try // trying to pause a service
{
serviceController1.Pause();
textBox1.Text = serviceController1.Status.ToString();
}
catch (Exception)// exception thrown
{
//handling exception
MessageBox.Show("Could not pause this process",
"Process failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
serviceController1.Close();
}
}
else if(serviceController1.Status.ToString()=="Paused")
{
Thread.Sleep(1500); // trying to pause a service
try
{
serviceController1.Stop();
textBox1.Text = "Stopped";
textBox1.BackColor = System.Drawing.Color.Red;// change textbox color to red when
// process status is changed
}
catch (Exception)// exception thrown
{
//handling exception
MessageBox.Show("Could not Stop this process",
"Process failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
serviceController1.Close();
}
}
else
{
Thread.Sleep(1000);
serviceController1.Start();
textBox1.BackColor = System.Drawing.Color.Red;// change textbox color to red when
// process status is changed
textBox1.Text = serviceController1.Status.ToString();
}
listBox1.Items.Add(serviceController1.ServiceName.ToString());
// listing all services that we tried to change their status.
}
请记住,我们选择的默认服务名称是 Appinfo
。如果未在服务控制器属性中选择服务名称,则可能会收到异常。
以下是一些应用程序截图
希望这篇文章对您有所帮助,让您了解如何控制已安装的服务。
注意
请随时留下关于这篇文章的任何评论或建议。