先新建一个winform项目,在拖入listView1控件

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

namespace 获取服务信息
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.listView1.Columns.Clear();
            this.listView1.Items.Clear();
            this.listView1.View = View.Details;
            this.listView1.FullRowSelect = true;
            listView1.Columns.Add("名称", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("描述", 300, HorizontalAlignment.Left);
            listView1.Columns.Add("状态", 100, HorizontalAlignment.Left);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ServiceController[] service = ServiceController.GetServices();
            for (int i = 0; i < service.Length; i++)
            {
                string name = service[i].ServiceName;
                string dname = service[i].DisplayName;
                string status = service[i].Status.ToString();

                ListViewItem items = new ListViewItem(name, 0);
                items.SubItems.Add(dname);
                switch (status.Trim())
                {
                    case "Stopped":
                        status = "已停止";
                        break;
                    case "Running":
                        status = "已启动";
                        break;
                    default:
                        status = "";
                        break;
                }
                items.SubItems.Add(status);
                listView1.Items.AddRange(new ListViewItem[] { items });
            }
        }
    }
}

posted on 2011-02-18 16:37  高兴happy  阅读(277)  评论(0编辑  收藏  举报