日期:2014-05-17  浏览次数:20804 次

控制windows服务,控件有3个button,1个datagridview
using System.ServiceProcess;
ServiceController[] sArr;
        ServiceController curs;
        public ServiceTest()
        {
            InitializeComponent();
        }
        private void ServiceTest_Load(object sender, EventArgs e)
        {
            Refresh1();
        }
        private void Refresh1()
        {
            sArr = ServiceController.GetServices();
            DataTable dt = Create();
            for (int i = 0; i < sArr.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = sArr[i].ServiceName;
                dr[1] = sArr[i].Status.ToString();
                dr[2] = sArr[i].DisplayName;
                dt.Rows.Add(dr);
            }
            this.dataGridView1.DataSource = dt;
        }
        private DataTable Create()
        {
            DataTable dt = new DataTable("dt");
            DataColumn dc = new DataColumn("name", typeof(string));
            dt.Columns.Add(dc);
            dc = new DataColumn("status", typeof(string));
            dt.Columns.Add(dc);
            dc = new DataColumn("des", typeof(string));
            dt.Columns.Add(dc);
            return dt;
        }
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView dg = sender as DataGridView;
            DataGridView.HitTestInfo hit = dg.HitTest(e.X, e.Y);
            switch (hit.Type)
            {
                case DataGridViewHitTestType.Cell:
                    string name = dataGridView1[0, hit.RowIndex].Value.ToString();