C# 服务控制

using  System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;

namespace 服务控制程序
{
public partial class Form1 : Form
{
bool tuchu=false
private ServiceController[] ServiceList;
public Form1()
{
InitializeComponent();
GetServiceList();
this.comboBox1.DataSource = ServiceList;
this.comboBox1.DisplayMember = "ServiceName"
}

private void GetServiceStatus()
{
//读取状态之前先进行刷新
ServiceController scl=(ServiceController)comboBox1.SelectedItem;

scl.Refresh();
//变更菜单项和图标
switch (scl.Status)
{
case System.ServiceProcess.ServiceControllerStatus.Paused:
button1.Enabled = false
开始ToolStripMenuItem.Enabled = false
button2.Enabled = false
暂停ToolStripMenuItem.Enabled = false
button3.Enabled = true
继续ToolStripMenuItem.Enabled = true
button4.Enabled = false
停止ToolStripMenuItem.Enabled = false
break
case System.ServiceProcess.ServiceControllerStatus.Running :
button1.Enabled = false
开始ToolStripMenuItem.Enabled = false
button2.Enabled = true
暂停ToolStripMenuItem.Enabled = true
button3.Enabled = false
继续ToolStripMenuItem.Enabled = false
button4.Enabled = true
停止ToolStripMenuItem.Enabled = true
break
case System.ServiceProcess.ServiceControllerStatus.Stopped:
button1.Enabled = true
开始ToolStripMenuItem.Enabled = true
button2.Enabled = false
暂停ToolStripMenuItem.Enabled = false
button3.Enabled = false
继续ToolStripMenuItem.Enabled = false
button4.Enabled = false
停止ToolStripMenuItem.Enabled = false
break
default:
button1.Enabled = false
开始ToolStripMenuItem.Enabled = false
button2.Enabled = false
暂停ToolStripMenuItem.Enabled = false
button3.Enabled = false
继续ToolStripMenuItem.Enabled = false
button4.Enabled = false
停止ToolStripMenuItem.Enabled = false
break
}
if (scl.CanPauseAndContinue==false)
{
button2.Enabled = false
暂停ToolStripMenuItem.Enabled = false
button3.Enabled = false
继续ToolStripMenuItem.Enabled = false
}
}

private bool GetServiceList()
{
ServiceList=ServiceController.GetServices();

return true
}

private void aToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
tuchu = true
notifyIcon1.Visible = false
Application.Exit();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (tuchu == false)
{
e.Cancel = true
this.Visible = false
}
}

private void Form1_Load(object sender, EventArgs e)
{
GetServiceStatus();
}

private void timer1_Tick(object sender, EventArgs e)
{
GetServiceStatus();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
GetServiceStatus();
}

private void button1_Click(object sender, EventArgs e)
{
ServiceController scl = (ServiceController)comboBox1.SelectedItem;
scl.Start();
}

private void button2_Click(object sender, EventArgs e)
{
ServiceController scl = (ServiceController)comboBox1.SelectedItem;
scl.Pause();
}

private void button3_Click(object sender, EventArgs e)
{
ServiceController scl = (ServiceController)comboBox1.SelectedItem;
scl.Continue();
}

private void button4_Click(object sender, EventArgs e)
{
ServiceController scl = (ServiceController)comboBox1.SelectedItem;
scl.Stop();
}
}
}

你可能感兴趣的:(C#)