原文using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace Windows_Service管理器
{
    public partial class Form1 : Form
    {
        private int index; //index用于记录所选的服务在listBox1中所在的位置
        private System.ServiceProcess.ServiceController[] watch;
        //watch代表Windows Service的监视器数组
        private System.ServiceProcess.ServiceController control = new
            System.ServiceProcess.ServiceController();
        //control代表Windows Service的控制器
        public Form1()
        {
            InitializeComponent();
        }        private void showAll_Click(object sender, EventArgs e)
        {
            //当按下showAll按钮时,在listBox1中显示全部的服务
            watch = System.ServiceProcess.ServiceController.GetServices();
            listBox1.DisplayMember = "DisplayName";
            listBox1.DataSource = watch;
        }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            control = (System.ServiceProcess.ServiceController)
            listBox1.SelectedItem;
            textBox1.Text = Control.DisplayName;
            switch (Control.Status)
            { /*当在listBox1中选中某项服务时,在textBox2中将显示出该服务的相应状态,并且将相应的按钮设为禁用:*/
                case System.ServiceProcess.ServiceControllerStatus.ContinuePending:
                    start.Enabled = false;
                    pause.Enabled = true;
                    continueAgain.Enabled = false;
                    stop.Enabled = true;
                    textBox2.Text = "服务将重启动";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.Paused:
                    start.Enabled = false;
                    pause.Enabled = false;
                    continueAgain.Enabled = true;
                    stop.Enabled = true;
                    textBox2.Text = "服务已被暂停";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.PausePending:
                    start.Enabled = false;
                    pause.Enabled = false;
                    continueAgain.Enabled = false;
                    stop.Enabled = true;
                    textBox2.Text = "服务即将被暂停";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.Running:
                    start.Enabled = false;
                    continueAgain.Enabled = true;
                    pause.Enabled = true;
                    stop.Enabled = true;
                    textBox2.Text = "服务正在运行";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.StopPending:
                    start.Enabled = true;
                    stop.Enabled = false;
                    pause.Enabled = false;
                    continueAgain.Enabled = false;
                    textBox2.Text = "服务即将终止";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.StartPending:
                    start.Enabled = false;
                    stop.Enabled = true;
                    pause.Enabled = true;
                    continueAgain.Enabled = true;
                    textBox2.Text = "服务即将开始";
                    break;
                case System.ServiceProcess.ServiceControllerStatus.Stopped:
                    start.Enabled = true;
                    stop.Enabled = false;
                    pause.Enabled = false;
                    continueAgain.Enabled = false;
                    textBox2.Text = "服务已经停止";
                    break;
                default:
                    start.Enabled = false;
                    continueAgain.Enabled = false;
                    stop.Enabled = false;
                    pause.Enabled = false;
                    textBox2.Text = "其他状态";
                    break;
            }            //如果某个服务不允许被暂停或重启动,则禁用暂停服务和继续服务按钮
            if (!control.CanPauseAndContinue)
            {
                pause.Enabled = false;
                continueAgain.Enabled = false;
            }
            //如果某个服务不允许被停止,则禁用停止服务按钮
            if (!control.CanStop)
            {
                stop.Enabled = false;
            }
        }        private void start_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            /* 因为启动服务需要一小段时间,所以当按下“开始服务”按钮时,应把光标的形式改变成等待状*/
            control = (System.ServiceProcess.ServiceController)
            listBox1.SelectedItem;
            control.Start();//启动服务
            control.WaitForStatus(ServiceControllerStatus.Running);//等待服务运行
            int index = listBox1.SelectedIndex;//记录所选的服务在listBox1中所在位置
            RefreshList();//因为服务的状态已经改变,所以要刷新服务列表
            listBox1.SelectedIndex = index;//将光标定位在所操作的服务位置上
            if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
                textBox2.Text = "正在运行";
            }
            Cursor.Current = Cursors.Default;/*服务已经开始运行,所以将光标的形状恢复成原状*/
        }        private void pause_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            controller = (System.ServiceProcess.ServiceController)
            listBox1.SelectedItem;
            controller.Pause();//暂停服务
            controller.WaitForStatus(ServiceControllerStatus.Paused);//等待服务被暂停
            int index = listBox1.SelectedIndex;
            RefreshList();
            listBox1.SelectedIndex = index;
            if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Paused)
            {
                textBox2.Text = "已被暂停";
            }
            Cursor.Current = Cursors.Default;
        }        private void continueAgain_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            controller = (System.ServiceProcess.ServiceController)
            listBox1.SelectedItem;
            controller.Continue();//重启动服务
            controller.WaitForStatus(ServiceControllerStatus.Running);//等待服务的重新启动
            int index = listBox1.SelectedIndex;
            RefreshList();
            listBox1.SelectedIndex = index;
            if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            {
                textBox2.Text = "正在运行";
            }
            Cursor.Current = Cursors.Default;
        }        private void stop_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            controller = (System.ServiceProcess.ServiceController)
            listBox1.SelectedItem;
            controller.Stop();//停止服务
            controller.WaitForStatus(ServiceControllerStatus.Stopped);//等待服务停止
            int index = listBox1.SelectedIndex;
            RefreshList();
            listBox1.SelectedIndex = index;
            if (controller.Status == System.ServiceProcess.ServiceControllerStatus.StopPending)
            {
                textBox2.Text = "已经停止";
            }
            Cursor.Current = Cursors.Default;
        }
    }
}

解决方案 »

  1.   

    错误
    错误1 命名空间“System”中不存在类型或命名空间名称“ServiceProcess”(是缺少程序集引用吗?) 
    错误2 命名空间“System”中不存在类型或命名空间名称“ServiceProcess”(是缺少程序集引用吗?) 
    错误 1 “System.Windows.Forms.Control”并不包含“DisplayName”的定义 
    错误 2 “System.Windows.Forms.Control”并不包含“Status”的定义 
    错误 3 当前上下文中不存在名称“ServiceControllerStatus” 
    错误 4 当前上下文中不存在名称“RefreshList” 
    错误 5 当前上下文中不存在名称“controller” 
    错误 6 当前上下文中不存在名称“controller” 
    错误 7 当前上下文中不存在名称“controller” 
    错误 8 当前上下文中不存在名称“controller” 
    错误 9 当前上下文中不存在名称“ServiceControllerStatus” 
    错误 10 当前上下文中不存在名称“RefreshList” 
    错误 11 当前上下文中不存在名称“controller” 
    错误 12 当前上下文中不存在名称“controller” 
    错误 13 当前上下文中不存在名称“controller” 
    错误 14 当前上下文中不存在名称“controller” 
    错误 15 当前上下文中不存在名称“ServiceControllerStatus” 
    错误 16 当前上下文中不存在名称“RefreshList” 
    错误 17 当前上下文中不存在名称“controller” 
    错误 18 当前上下文中不存在名称“controller” 
    错误 19 当前上下文中不存在名称“controller” 
    错误 20 当前上下文中不存在名称“controller” 
    错误 21 当前上下文中不存在名称“ServiceControllerStatus” 
    错误 22 当前上下文中不存在名称“RefreshList” 
    错误 23 当前上下文中不存在名称“controller” 
      

  2.   

    修改后  
    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 ServiceWatchControl
    {
        public partial  class Form1 : System.Windows .Forms.Form
        {
          
            private System.ServiceProcess.ServiceController[] watch;
            private System.ServiceProcess.ServiceController control = new System.ServiceProcess.ServiceController();
            public Form1()
            {
                InitializeComponent();
            }
           
                    private void showAll_Click(object sender, EventArgs e)
            {
                watch = System.ServiceProcess.ServiceController.GetServices();
                listBox1.DisplayMember = "DisplayerName";
                listBox1.DataSource = watch;
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void showAll_Click()
            {        }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                control = (System.ServiceProcess.ServiceController)
                 listBox1.SelectedItem;
                textBox1.Text = control.DisplayName;
                switch (control.Status)
                { case  System.ServiceProcess.ServiceControllerStatus.ContinuePending:
                        start .Enabled =false ;
                        pause .Enabled =true;
                        continueAgain.Enabled =false ;
                        stop .Enabled =true ;
                        textBox2 .Text ="服务将重启动";
                        break ;                case  System.ServiceProcess.ServiceControllerStatus.Paused:
                        start .Enabled =false ;
                        pause .Enabled =false ;
                        continueAgain.Enabled =true  ;
                        stop .Enabled =true ;
                        textBox2 .Text ="服务已被暂停";
                        break ;                case  System.ServiceProcess.ServiceControllerStatus.PausePending:
                        start .Enabled =false ;
                        pause .Enabled =false ;
                        continueAgain.Enabled =false ;
                        stop .Enabled =true ;
                        textBox2 .Text ="服务将被暂停";
                        break ;
                 
                     
                    case  System.ServiceProcess.ServiceControllerStatus.Running:
                        start .Enabled =false ;
                        pause .Enabled =true;
                        continueAgain.Enabled =true  ;
                        stop .Enabled =true ;
                        textBox2 .Text ="服务正在运行";
                        break ;
                    case  System.ServiceProcess.ServiceControllerStatus.StopPending:
                        start .Enabled =true  ;
                        pause .Enabled =false ;
                        continueAgain.Enabled =false ;
                        stop .Enabled =false  ;
                        textBox2 .Text ="服务即将停止";
                        break ;
                    case  System.ServiceProcess.ServiceControllerStatus.StartPending:
                        start .Enabled =false ;
                        pause .Enabled =true;
                        continueAgain.Enabled =true  ;
                        stop .Enabled =true ;
                        textBox2 .Text ="服务即将开始";
                        break ;
                    case  System.ServiceProcess.ServiceControllerStatus.Stopped:
                        start .Enabled =true  ;
                        pause .Enabled =false ;
                        continueAgain.Enabled =false ;
                        stop .Enabled =false  ;
                        textBox2 .Text ="服务已经停止";
                        break ;
                    default :
                        start .Enabled =false ;
                        pause .Enabled =false ;
                        continueAgain.Enabled =false ;
                        stop .Enabled =false  ;
                        textBox2 .Text ="状态其他";
                        break ;
                }
                if (!control.CanPauseAndContinue)
                {
                    pause.Enabled = false;
                    continueAgain.Enabled = false;
                }
                if (!control.CanStop)
                {
                    stop.Enabled = false;
                }        }        private void start_Click(object sender, EventArgs e)
            {
                Cursor.Current = Cursors.WaitCursor;
                control = (System.ServiceProcess.ServiceController)
                     listBox1.SelectedItem;
                try
                {
                    control.Start();
                }
                catch (System.InvalidOperationException )
                {
                    Console.WriteLine("无法调用");
                    return ;
                }
                control.WaitForStatus(ServiceControllerStatus.Running);
                int index = listBox1.SelectedIndex;
                Refresh();
                listBox1.SelectedIndex = index;
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    textBox2.Text = "正在运行";
                }
                Cursor.Current = Cursors.Default;        }        private void pause_Click(object sender, EventArgs e)
            {
                Cursor.Current = Cursors.WaitCursor;
                control = (System.ServiceProcess.ServiceController)
                     listBox1.SelectedItem;
                try
                {
                    control.Start();
                }
                catch (System.InvalidOperationException)
                {
                    Console.WriteLine("无法调用");
                    return;
                }
                control.WaitForStatus(ServiceControllerStatus.Paused);
                int index = listBox1.SelectedIndex;
                  Refresh();
                listBox1.SelectedIndex = index;
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.Paused)
                {
                    textBox2.Text = "已被暂停";
                }
                Cursor.Current = Cursors.Default;        }        private void continueAgain_Click(object sender, EventArgs e)
            {
                Cursor.Current = Cursors.WaitCursor;
                control = (System.ServiceProcess.ServiceController)
                     listBox1.SelectedItem;
                try
                {
                    control.Start();
                }
                catch (System.InvalidOperationException)
                {
                    Console.WriteLine("无法调用");
                    return;
                }
                control.WaitForStatus(ServiceControllerStatus.Running);
                int index = listBox1.SelectedIndex;
                Refresh();
                listBox1.SelectedIndex = index;
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    textBox2.Text = "正在运行";
                }
                Cursor.Current = Cursors.Default;        }
            private void button5_Click(object sender, EventArgs e)
            {
                Cursor.Current = Cursors.WaitCursor;
                control = (System.ServiceProcess.ServiceController)
                     listBox1.SelectedItem;
                try
                {
                    control.Start();
                }
                catch (System.InvalidOperationException)
                {
                    Console.WriteLine("无法调用");
                    return;
                }
                control.WaitForStatus(ServiceControllerStatus.Stopped);
                int index = listBox1.SelectedIndex;
                Refresh();
                listBox1.SelectedIndex = index;
                if (control.Status == System.ServiceProcess.ServiceControllerStatus.StopPending)
                {
                    textBox2.Text = "已经停止";
                }
                Cursor.Current = Cursors.Default;        }
        }
    }
      

  3.   

    错误1 命名空间“System”中不存在类型或命名空间名称“ServiceProcess”(是缺少程序集引用吗?) 
    在解决方案管理器的 引用中 添加 ServiceProcess.dll  就好了