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();
        }
    }
}
/*--------------------------------------------------------------------------------*/
posted @ 2012-02-28 21:11  kay2010hh  阅读(586)  评论(2编辑  收藏  举报