SQLServer服务管理器

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

namespace 启动或者停止Windows服务
{
    public partial class Form1 : Form
    {
        string tempServer;
        string tempProcess;
        public Form1()
        {
            InitializeComponent();
            try
            {
                tempServer = System.Configuration.ConfigurationManager.AppSettings["servername"];
                tempProcess = System.Configuration.ConfigurationManager.AppSettings["processname"];//用来从配置文件XML中读取配置数据
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSartSQLServer_Click(object sender, EventArgs e)//启动服务
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                if (sc.Status.Equals(ServiceControllerStatus.Running))//判断服务是否运行
                {
                    MessageBox.Show("服务正在运行中,不用手动启动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    label1.Text = "启动中,请稍候......";
                    Thread t = new Thread(Start);//开一个新线程,用来启动服务,这样窗口不会假死
                    t.Start();
                }
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Start()//启动服务的函数
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                sc.Start();
                string name = sc.ServiceName;//获取服务运行时名称
                Thread.Sleep(4000);//休眠四秒,等待服务启动
                label1.Text = " 服务成功启动";
                Process[] process = Process.GetProcessesByName(tempProcess);//开进程用来获取进程的PID
                Process p = process[0];
                int num = p.Id;
                label2.Text = "服务名为 " + name + " ,PID为" + num;
                MessageBox.Show("启动成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void btnStopSQLServer_Click(object sender, EventArgs e)//停止服务
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                if (sc.Status.Equals(ServiceControllerStatus.Stopped))//判断服务是否停止
                {
                    MessageBox.Show("服务本身停止,不用手动停止", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    label1.Text = "停止中,请稍候......";
                    Thread t = new Thread(Stop);
                    t.Start();
                }
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void Stop()//停止服务函数
        {
            try
            {
                ServiceController sc = new ServiceController(tempServer);
                sc.Stop();
                Thread.Sleep(4000);//休眠四秒等待服务停止
                label1.Text = "服务成功停止";
                label2.Text = "";
                MessageBox.Show("停止成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_Load(object sender, EventArgs e)//窗体加载事件
        {
            try
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                ServiceController sc = new ServiceController(tempServer);
                if (sc.Status.Equals(ServiceControllerStatus.Running))
                {
                    label1.Text = "服务已经正在运行";
                    Process[] process = Process.GetProcessesByName(tempProcess);
                    Process p = process[0];
                    int num = p.Id;
                    string name = sc.ServiceName;
                    label2.Text = "服务名称为 " + name + ", PID为" + num;
                }
                else
                    label1.Text = "服务已经停止,请启动服务";
            }
            catch
            {
                MessageBox.Show("配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SQLServer服务管理器.AboutBox1 a = new SQLServer服务管理器.AboutBox1();
            a.ShowDialog();
        }
    }
}
/*--------------------------------------------------------------------------------*/

转载于:https://www.cnblogs.com/kay2010hh/archive/2012/02/28/2372427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值