使用ServiceController控制windows服务

来源:互联网 发布:3d直选遗漏数据统计 编辑:程序博客网 时间:2024/05/17 06:41

ServiceController控制windows服务

1.在项目中“引用”=》.net选项中找到System.ServiceProcess

2.using System.ServiceProcess;

//启动服务

        private void button13_Click(object sender, EventArgs e)        {            try            {                ServiceController sc = new ServiceController("WapSearchWCFService");                if (sc.Status == ServiceControllerStatus.Stopped)                {                    sc.Start();                    sc.WaitForStatus(ServiceControllerStatus.Running);                }                sc.Close();                this.label27.Text = "已启动";            }            catch            {                MessageBox.Show("启动失败!");            }        }


//停止服务

        private void button14_Click(object sender, EventArgs e)        {            try            {                ServiceController sc = new ServiceController("WapSearchWCFService");                if (sc.Status == ServiceControllerStatus.Running)                {                    sc.Stop();                    sc.WaitForStatus(ServiceControllerStatus.Stopped);                }                sc.Close();                this.label27.Text = "已停止";            }            catch            {                MessageBox.Show("停止失败!");            }        }


 

//重新启动

 private void button15_Click(object sender, EventArgs e)        {            try            {                ServiceController sc = new ServiceController("WapSearchWCFService");                if (sc.Status == ServiceControllerStatus.Running)                {                    sc.Stop();                    sc.WaitForStatus(ServiceControllerStatus.Stopped);                    this.label27.Text = "已停止";                }                if (sc.Status == ServiceControllerStatus.Stopped)                {                    sc.Start();                    sc.WaitForStatus(ServiceControllerStatus.Running);                    this.label27.Text = "已启动";                }                sc.Close();                MessageBox.Show("重新启动成功!");            }            catch            {                MessageBox.Show("重新启动失败!");            }        }


 

 


 

2 0