C# 服务管理器 源代码
C# 服务管理器 源代码
// 源程序参考: // https://code.msdn.microsoft.com/Working-with-Windows-6ff7877c using System; using System.Collections.Generic; using System.Windows.Forms; // 需要手工在“解决方案资源管理器”下的“引用”中添加 "System.ServiceProcess" using System.ServiceProcess; namespace ServiceManager { public partial class Form1 : Form { // 用来保存现在选择的服务实例 private ServiceController msvc; private SortedList<string, ServiceController> controllers = new SortedList<string, ServiceController>(); public Form1() { InitializeComponent(); } private void EnumServices() { // 获取可用的服务列表并将它们装入 listView try { toolStrip1.Text = "添加服务列表,请稍候 . . . "; lvServices.Items.Clear(); if (controllers != null) controllers = new SortedList<string, ServiceController>(); ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController controller in services) { // controller.ServiceName 服务名称 var _with1 = lvServices.Items.Add(controller.DisplayName); _with1.SubItems.Add(controller.Status.ToString()); _with1.SubItems.Add(controller.ServiceType.ToString()); controllers.Add(controller.DisplayName, controller); } } catch (Exception exp) { MessageBox.Show("不能列举服务. "); // System.Diagnostics.Trace.WriteLine("不能列举服务. "); } finally { toolStrip1.Text = "已就绪"; } } private void Form1_Load(object sender, EventArgs e) { EnumServices(); } private void UpdateServiceStatus() { // 检查每个服务 try { toolStrip1.Text = "检查服务状态 . . ."; foreach (ListViewItem item in lvServices.Items) { msvc = controllers[item.Text]; msvc.Refresh(); item.SubItems[1].Text = msvc.Status.ToString(); } UpdateUIForSelectedService(); } catch (Exception exp) { MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { toolStrip1.Text = "已就绪"; } } private void UpdateUIForSelectedService() { // 依据被选择的服务项修改界面上的按钮状态. string strName = null; try { if (lvServices.SelectedItems.Count == 1) { strName = lvServices.SelectedItems[0].SubItems[0].Text; msvc = controllers[strName]; var _with2 = msvc; lvServices.SelectedItems[0].SubItems[1].Text = _with2.Status.ToString(); // 如果它是停止状态,我们应该可以启动它 this.cmdStart.Enabled = (_with2.Status == ServiceControllerStatus.Stopped); // 确定我们可以停止它,而且它现在并未停止 this.cmdStop.Enabled = (_with2.CanStop && (!(_with2.Status == ServiceControllerStatus.Stopped))); // 确定我们可以暂停它,而且它现在并未暂停 this.cmdPause.Enabled = (_with2.CanPauseAndContinue && (!(_with2.Status == ServiceControllerStatus.Paused))); // 如果它是暂停状态,我们应该可以继续运行它 this.cmdResume.Enabled = (_with2.Status == ServiceControllerStatus.Paused); } } catch (Exception exp) { MessageBox.Show("不能更新用户界面.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cmdPause_Click(object sender, EventArgs e) { try { msvc.Pause(); msvc.WaitForStatus(ServiceControllerStatus.Paused); UpdateUIForSelectedService(); } catch (Exception exp) { MessageBox.Show("不能暂停服务.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cmdResume_Click(object sender, EventArgs e) { try { msvc.Continue(); msvc.WaitForStatus(ServiceControllerStatus.Running); UpdateUIForSelectedService(); } catch (Exception exp) { MessageBox.Show("不能继续服务.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cmdStart_Click(object sender, EventArgs e) { try { msvc.Start(); msvc.WaitForStatus(ServiceControllerStatus.Running); UpdateUIForSelectedService(); } catch (Exception exp) { MessageBox.Show("不能启动服务.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void cmdStop_Click(object sender, EventArgs e) { try { msvc.Stop(); msvc.WaitForStatus(ServiceControllerStatus.Stopped); UpdateUIForSelectedService(); } catch (Exception exp) { MessageBox.Show("不能停止服务.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void lvServices_SelectedIndexChanged(object sender, EventArgs e) { UpdateUIForSelectedService(); } private void cmdExit_Click(object sender, EventArgs e) { Close(); } private void CmdRefresh_Click(object sender, EventArgs e) { UpdateServiceStatus(); } } }
浙公网安备 33010602011771号