C# - Система
Класс для работы со службами Windows
Автор:
Грег Дубиновский | добавлено: 01.02.2011, 12:41 | просмотров: 7030 (1+) | комментариев:
0 | рейтинг:
x10
Класс позволят запускать, перезапускать, останавливать службы Windows на указанном компьютере.
Код
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.Win32;
using System.Security.Principal;
using System.ServiceProcess;
using System.Diagnostics;
namespace SiccoloWebServiceEventProcessor
{
/// <summary>
/// Summary description for NTServiceInfo.
/// </summary>
public class NTServiceInfo
{
private string m_ServiceName="";
private string m_MachineName = "";
public NTServiceInfo(string NTServiceName, string MachineName)
{
m_ServiceName = NTServiceName;
m_MachineName = MachineName;
}
public string ServiceStatus()
{
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
{Service = new ServiceController(this.m_ServiceName, this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
string ServiceStatus = Service.Status.ToString();
Service.Close();
return ServiceStatus;
}
public bool CanStop()
{
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
{Service = new ServiceController(this.m_ServiceName, this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
bool CanStop = Service.CanStop;
Service.Close();
return CanStop;
}
public bool CanPauseAndContinue()
{
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
{Service = new ServiceController(this.m_ServiceName, this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
bool CanPauseAndContinue = Service.CanPauseAndContinue;
Service.Close();
return CanPauseAndContinue;
}
public bool StopService(WindowsPrincipal User, bool ToDebug, out string ErrorInfo)
{
try
{
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
{Service = new ServiceController(this.m_ServiceName, this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
//stop all the Dependent services...
foreach (System.ServiceProcess.ServiceController dependent_service in Service.DependentServices)
{
switch ( dependent_service.Status)
{
case ServiceControllerStatus.Stopped:
//already stopped...nothing to do
break;
case ServiceControllerStatus.StopPending:
dependent_service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
default:
Service.Stop();
Service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
}
}
//stop main service...
switch ( Service.Status)
{
case ServiceControllerStatus.Stopped:
//already stopped...nothing to do
break;
case ServiceControllerStatus.StopPending:
Service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
default:
// *-*************************************************************************
//check all dependent services?
foreach (System.ServiceProcess.ServiceController dependent_service in Service.DependentServices)
{
switch ( dependent_service.Status)
{
case ServiceControllerStatus.Stopped:
//already stopped...nothing to do
break;
case ServiceControllerStatus.StopPending:
dependent_service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
default:
Service.Stop();
Service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
}
}
// *-*************************************************************************
if ( !Service.CanStop )
{
throw new Exception ("Cannot stop service [" + this.m_ServiceName + "]");
}
Service.Stop();
Service.WaitForStatus(ServiceControllerStatus.Stopped);
break;
}
Service.Close();
impersonationContext.Undo();
ErrorInfo="";
return true;
}
catch (Exception ex_stop_service)
{
ErrorInfo = ex_stop_service.Message;
return false;
}
}
public bool StartService(WindowsPrincipal User, bool ToDebug, out string ErrorInfo)
{
try
{
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
System.ServiceProcess.ServiceController Service;
if (this.m_MachineName!="")
{Service = new ServiceController(this.m_ServiceName, this.m_MachineName ) ;}
else
{Service = new ServiceController(this.m_ServiceName ) ;}
switch ( Service.Status)
{
case ServiceControllerStatus.Stopped:
Service.Start();
Service.WaitForStatus(ServiceControllerStatus.Running);
break;
case ServiceControllerStatus.StopPending:
//wait for it to stop
Service.WaitForStatus(ServiceControllerStatus.Stopped);
//... and then start
Service.Start();
Service.WaitForStatus(ServiceControllerStatus.Running);
break;
case ServiceControllerStatus.StartPending:
//nothing to do...just wait
Service.WaitForStatus(ServiceControllerStatus.Running);
break;
case ServiceControllerStatus.Running:
//nothing to do.already running...
break;
default:
Service.Start();
Service.WaitForStatus(ServiceControllerStatus.Running);
break;
}
//start all the Dependent services...
foreach (System.ServiceProcess.ServiceController dependent_service in Service.DependentServices)
{
switch (dependent_service.Status )
{
case ServiceControllerStatus.StartPending:
//just wait for it to start
dependent_service.WaitForStatus (ServiceControllerStatus.Running);
break;
case ServiceControllerStatus.Running:
//already running.nothing to do
break;
default:
NTServiceInfo si = new NTServiceInfo( dependent_service.ServiceName, this.m_MachineName);
if ( !si.StartService(User, ToDebug, out ErrorInfo))
{
throw new Exception ("Failed to start Dependent Service [" + dependent_service.ServiceName +
"] - Error [" + ErrorInfo + "]") ;
}
break;
}
}
Service.Close();
impersonationContext.Undo();
ErrorInfo="";
return true;
}
catch (Exception ex_start_service)
{
ErrorInfo = ex_start_service.Message;
return false;
}
}
public bool RestartServervice (WindowsPrincipal User,
bool ToDebug,
out string ErrorInfo)
{
try
{
ErrorInfo ="";
if (this.StopService(User, ToDebug, out ErrorInfo))
{
if (this.StartService(User, ToDebug, out ErrorInfo))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
catch (Exception ex_restart_service)
{
ErrorInfo = "Restart Service [" + this.m_ServiceName + "] [" + ex_restart_service.Message + "]";
return false;
}
}
}
}
/*
Использовать, например так:
public bool RestartSQLServerService(string RemoteServerAddress, out string NewServiceStatus, out string ErrorInfo )
{
try
{
string ToDebugSetting = System.Configuration.Configura tionSettings.AppSettings.Get(& quot;DebugMode");
bool ToDebug = (ToDebugSetting!="") ;
string NTServiceName = "MSSQLSERVER";
ErrorInfo="";
NewServiceStatus ="";
System.Security.Principal.Wind owsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.Wi ndowsIdentity)User.Identity).I mpersonate();
NTServiceInfo si = new NTServiceInfo(NTServiceName, RemoteServerAddress);
if ( ! si.RestartServervice ((WindowsPrincipal) this.User,ToDebug, out ErrorInfo))
{return false;}
NewServiceStatus = si.ServiceStatus();
impersonationContext.Undo();
ErrorInfo = "";
return true;
}
catch (Exception ex_get_service_info)
{
NewServiceStatus ="";
ErrorInfo = ex_get_service_info.Message;
return false;
}
}
*/