2 Star 20 Fork 19

accbear/WebRunLocal

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ServiceManager.cs 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
sharkidon 提交于 2019-02-11 20:50 +08:00 . Initial commit
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace WRLClient
{
class ServiceManager
{
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName">服务名称</param>
/// <returns></returns>
public static bool isServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
/// <summary>
/// 安装服务
/// </summary>
/// <param name="serviceFilePath">程序路径</param>
public static void installService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="serviceFilePath">程序路径</param>
public static void uninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName">服务名称</param>
public static void serviceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="serviceName">服务名称</param>
public static void serviceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/accbear/WebRunLocal.git
git@gitee.com:accbear/WebRunLocal.git
accbear
WebRunLocal
WebRunLocal
master

搜索帮助