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;
Start or stop the Windows service using namespace
{
Public partial class Form1: Form
{
String tempServer;
String tempProcess;
Public Form1 ()
{
InitializeComponent ();
Try
{
TempServer = System. Configuration. ConfigurationManager. receivettings ["servername"];
TempProcess = System. Configuration. ConfigurationManager. AppSettings ["processname"]; // used to read Configuration data from the Configuration file XML
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Private void btnSartSQLServer_Click (object sender, EventArgs e) // start the service
{
Try
{
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Running) // judge whether the service is Running
{
MessageBox. Show ("the service is running, do not start manually", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
Label1.Text = "starting... please wait ......";
Thread t = new Thread (Start); // open a new Thread to Start the service, so that the window will not be suspended
T. Start ();
}
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Public void Start () // The function used to Start the service.
{
Try
{
ServiceController SC = new ServiceController (tempServer );
SC. Start ();
String name = SC. ServiceName; // get the service runtime name
Thread. Sleep (4000); // Sleep for four seconds, waiting for the service to start
Label1.Text = "service started successfully ";
Process [] process = Process. GetProcessesByName (tempProcess); // run the Process to obtain the PID of the process.
Process p = process [0];
Int num = p. Id;
Label2.Text = "service name" + name + ", PID is" + num;
MessageBox. Show ("started successfully", "succeeded", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Private void btnStopSQLServer_Click (object sender, EventArgs e) // stop the service
{
Try
{
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Stopped) // determines whether the service is Stopped.
{
MessageBox. Show ("the service itself stops, do not stop manually", "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
Label1.Text = "Stopping... please wait ......";
Thread t = new Thread (Stop );
T. Start ();
}
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Public void Stop () // Stop a service function
{
Try
{
ServiceController SC = new ServiceController (tempServer );
SC. Stop ();
Thread. Sleep (4000); // wait for the service to stop after four seconds of Sleep
Label1.Text = "Service stopped successfully ";
Label2.Text = "";
MessageBox. Show ("stopped successfully", "succeeded", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Private void Form1_Load (object sender, EventArgs e) // form loading event
{
Try
{
Control. checkforillegalcrossthreadcils = false;
ServiceController SC = new ServiceController (tempServer );
If (SC. Status. Equals (ServiceControllerStatus. Running ))
{
Label1.Text = "the service is running ";
Process [] process = Process. GetProcessesByName (tempProcess );
Process p = process [0];
Int num = p. Id;
String name = SC. ServiceName;
Label2.Text = "service name:" + name + ", PID:" + num;
}
Else
Label1.Text = "the Service has stopped. Please start the service ";
}
Catch
{
MessageBox. Show ("configuration file loss", "error", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Private void button#click (object sender, EventArgs e)
{
SQLServer service manager. AboutBox1 a = new SQLServer service manager. AboutBox1 ();
A. ShowDialog ();
}
}
}
/*--------------------------------------------------------------------------------*/
From kay2010hh