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();
}
}
}
/*--------------------------------------------------------------------------------*/