CSharp操作服务
CSharp操作服务的方法,包括启动服务对象,停止服务,为了避免异常,执行前先判断状态
CSharp操作服务的方法,包括启动服务对象,停止服务,为了避免异常,执行前先判断状态
/// <summary>
/// 启动服务
/// </summary>
public static void StartServerStatus()
{
ServiceController sc = new ServiceController();
sc.ServiceName = "ServerName";
if (sc.Status != ServiceControllerStatus.Running)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
}
/// <summary>
/// 停止服务
/// </summary>
public static void StopServerStop()
{
ServiceController sc = new ServiceController();
sc.ServiceName = "ServerName";
if (sc.Status != ServiceControllerStatus.Stopped)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
}
}