Starting and Stopping a service using a Windows Application
Steps
0. Create a windows Form in a Windows Application1. First add the ServiceProcess.dll (as a .Net reference to the project)
// Add this
2. using System.ServiceProcess;
// Class level object
3. private ServiceController objServiceController;
// Constructor
4. public Form1() { // // Required for Windows Form Designer support // InitializeComponent();
objServiceController = new ServiceController ("ServiceTest", "MPHWIME009");
}
5.
private void btnStartService_Click(object sender, System.EventArgs e) { if(objServiceController.Status == ServiceControllerStatus.Stopped) { objServiceController.Start(); objServiceController.Refresh (); } }
6.
private void btnStopService_Click(object sender, System.EventArgs e) { if(objServiceController.Status == ServiceControllerStatus.Running) { objServiceController.Stop(); objServiceController.Refresh (); } }
0 Comments:
Post a Comment
<< Home