C#编写简单的系统服务管理器

从网上看到了一段关于服务枚举的文章,于是做了做搬家的工作,代码基本是照搬了,练手而已,路过看客切勿砸砖。

程序截图:

 

代码如下:

代码
  1 using System;
  2 using System.Windows.Forms;
  3 using System.ServiceProcess;
  4 using System.Collections;
  5 
  6 namespace ServiceManage
  7 {
  8     public partial class MainForm : Form
  9     {
 10         public MainForm()
 11         {
 12             InitializeComponent();
 13         }
 14 
 15         private Hashtable htSvcs = new Hashtable();
 16 
 17         private void MainForm_Load(object sender, EventArgs e)
 18         {
 19             EnumServices();
 20         }
 21 
 22         private void cbAutoReflashStatus_CheckedChanged(object sender, EventArgs e)
 23         {
 24             if(cbAutoReflashStatus.Checked)
 25             {
 26                 tmrStatus.Enabled = true;
 27             }
 28             else
 29             {
 30                 tmrStatus.Enabled = false;
 31             }
 32         }
 33 
 34         /// <summary>
 35         /// 更新服务状态
 36         /// </summary>
 37         private  void UpdateServiceStatus()
 38         {
 39             ServiceController svc;
 40             foreach (ListViewItem lvi in lvService.Items)
 41             {
 42                 svc = (ServiceController) htSvcs[lvi.Text];
 43                 svc.Refresh();
 44                 lvi.SubItems[2].Text = svc.Status.ToString();
 45             }
 46         }
 47 
 48         private void tmrStatus_Tick(object sender, EventArgs e)
 49         {
 50             UpdateServiceStatus();
 51         }
 52 
 53         /// <summary>
 54         /// 枚举系统服务
 55         /// </summary>
 56         private void EnumServices()
 57         {
 58             lvService.Items.Clear();
 59             ListViewItem lvItem;
 60             ServiceController[] svcs = ServiceController.GetServices();
 61             foreach (ServiceController svc in svcs)
 62             {
 63                 lvItem = lvService.Items.Add(svc.DisplayName);
 64                 lvItem.SubItems.Add(svc.ServiceName);
 65                 lvItem.SubItems.Add(svc.Status.ToString());
 66                 lvItem.SubItems.Add(svc.ServiceType.ToString());
 67                 htSvcs.Add(svc.DisplayName,svc);
 68             }
 69         }
 70 
 71         private void btnReverse_Click(object sender, EventArgs e)
 72         {
 73             foreach (ListViewItem lvi in lvService.Items)
 74             {
 75                 lvi.Checked = !lvi.Checked;
 76             }
 77         }
 78 
 79         private void btnSelAll_Click(object sender, EventArgs e)
 80         {
 81             foreach (ListViewItem lvi in lvService.Items)
 82             {
 83                 lvi.Checked = true;
 84             }
 85         }
 86 
 87         private void btnRun_Click(object sender, EventArgs e)
 88         {
 89             if(lvService.CheckedItems.Count>0)
 90             {
 91                 ServiceController svc;
 92                 foreach (ListViewItem lvi in lvService.CheckedItems)
 93                 {
 94                     svc = (ServiceController)htSvcs[lvi.Text];
 95                     if(svc.Status== ServiceControllerStatus.Stopped)  svc.Start();
 96                 }
 97                 UncheckSelected();
 98                 UpdateServiceStatus();
 99             }
100         }
101 
102         private void btnStop_Click(object sender, EventArgs e)
103         {
104             if (lvService.CheckedItems.Count > 0)
105             {
106                 ServiceController svc;
107                 foreach (ListViewItem lvi in lvService.CheckedItems)
108                 {
109                     svc = (ServiceController)htSvcs[lvi.Text];
110                     if ((svc.Status != ServiceControllerStatus.Stopped) && svc.CanStop) svc.Stop();
111                 }
112                 UncheckSelected();
113                 UpdateServiceStatus();
114             }
115         }
116         private void UncheckSelected()
117         {
118             foreach (ListViewItem lvi in lvService.CheckedItems)
119             {
120                 lvi.Checked = false;
121             } 
122         }
123     }
124 }

 

posted @ 2009-12-21 00:03  shaoyun  阅读(895)  评论(1编辑  收藏  举报