C#操作windows服务,安装、卸载、停止、启动

时间:2021-12-26 06:20:38
    public class ServiceUtil
{
private string _ServiceName = string.Empty;
private string _AppName = string.Empty; public string AppName
{
get { return _AppName; }
set { _AppName = value; }
} public string ServiceName
{
get { return _ServiceName; }
} /// <summary>
///
/// </summary>
/// <param name="appName">后缀名为.exe的文件</param>
/// <param name="serviceName"></param>
public ServiceUtil(string appName, string serviceName)
{
_AppName = appName;
_ServiceName = serviceName;
} #region 启动服务
/// <summary>
/// StartService
/// </summary>
public void StartService()
{
ServiceController sc = new ServiceController(_ServiceName);
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
}
}
#endregion #region 停止服务
/// <summary>
/// 停止服务
/// </summary>
public void StopService()
{
ServiceController sc = new ServiceController(_ServiceName);
if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();
}
} #endregion #region 安装服务
/// <summary>
/// 安装服务
/// </summary>
public void InstallService()
{
if (!isServiceIsExisted(_ServiceName))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(, location.LastIndexOf('\\') + ) + string.Format("{0}.exe", _AppName);
InstallmyService(null, serviceFileName);
}
}
#endregion #region 卸载服务
public void UnInstallService()
{
if (isServiceIsExisted(_ServiceName))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(, location.LastIndexOf('\\') + ) + string.Format("{0}.exe", _AppName);
UnInstallmyService(serviceFileName);
}
}
#endregion #region 检查服务存在的存在性
/// <summary>
/// 检查服务存在的存在性
/// </summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool isServiceIsExisted(string NameService)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName.ToLower() == NameService.ToLower())
{
return true;
}
}
return false;
}
#endregion #region Private
#region 安装Windows服务
/// <summary>
/// 安装Windows服务
/// </summary>
/// <param name="stateSaver">集合</param>
/// <param name="filepath">程序文件路径</param>
private void InstallmyService(IDictionary stateSaver, string filepath)
{ AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Install(stateSaver); AssemblyInstaller1.Commit(stateSaver); AssemblyInstaller1.Dispose(); }
#endregion #region 卸载Windows服务
/// <summary>
/// 卸载Windows服务
/// </summary>
/// <param name="filepath">程序文件路径</param>
private void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(null); AssemblyInstaller1.Dispose(); }
#endregion
#endregion }

测试环境:win8+vs2012+.net4.0