Appliction for Start and Stop windows Service in C#.Net

Here explaining how to create an application to control Windows Services on our local Computer and also on remote computers. You can simultaneously Stop or Start multiple Services on the local computer or on the specified remote computer.

Note: You will need to have permissions to control windows services.


Make use of the ServiceController class found in the System.ServiceProcess namespace of the .Net Framework. This class is using to connect to a windows service, to manipulate the service and get information about service.


C#.Net code :

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ServiceProcess;
using System.Net;

namespace WindowsServiceExample

{
Class ClwindowsService
{
ServiceController controller = new ServiceController();

Public void RestartService()
{
try
{

controller.MachineName = “Rag1001";
controller.ServiceName = "BulckCopy";
If (StartService == -1) return;
If (StopService == -1) return;
Response.Write(“Service Re-started Successfully”);

}
catch(Exception err )
{
Response.Write(err.Message);
}
}

Private int StartService()
{
try
{
If (controller.Status == ServiceControllerStatus. Stopped)
{
controller.Start();
}
Return 0;
}
catch(Exception err )
{
Return -1;
}
}
Private int StopService()
{
try
{
If (controller.Status == ServiceControllerStatus. Running)
{
controller.Stop();
}
Return 0;
}
catch(Exception err )
{
Return -1;
}
}
}
}

No comments: