博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET服务安装、卸载、启动、停止、判断是否存在
阅读量:7088 次
发布时间:2019-06-28

本文共 2724 字,大约阅读时间需要 9 分钟。

一、安装服务:

private void InstallService(IDictionary stateSaver, string filepath)
{
try
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");
if(!ServiceIsExisted("ServiceName"))
{
//Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path =filepath;
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
//--Start Service
service.Start();
}
else
{
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
}
}
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
}

二、卸载windows服务:

private void UnInstallService(string filepath)
{
try
{
if (ServiceIsExisted("ServiceName"))
{
//UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError/n" + ex.Message);
}
}

三、判断window服务是否存在:

private bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}

四、启动服务:

private void StartService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
throw new Exception(startServiceError.Replace("$s$", serviceName));
}
}
}
}
}

五、停止服务:

private void StopService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
throw new Exception(stopServiceError.Replace("$s$", serviceName));
}
}
}
}
}

注:手动安装window服务的方法:

转载地址:http://rbwql.baihongyu.com/

你可能感兴趣的文章
input 获取当前id,name
查看>>
linux zip 命令详解
查看>>
BZOJ3834 : [Poi2014]Solar Panels
查看>>
探索 OpenStack 之(8):Neutron 深入探索之 OVS + GRE 之 完整网络流程 篇
查看>>
Android Animation学习笔记
查看>>
Java多线程编程模式实战指南(二):Immutable Object模式--转载
查看>>
document.body.clientHeight的取值
查看>>
一行代码远离Google浏览器兼容问题的困扰
查看>>
【Win 10应用开发】自定义浮动层——Flyout
查看>>
xamarin 断点 不命中
查看>>
Android 性能优化之使用MAT分析内存泄露
查看>>
【小言的设计模式】类之间的关系
查看>>
ecshop变量介绍
查看>>
单点登录 SSO 的实现原理
查看>>
vs2010 SetUp 安装软件时,界面出现乱码的问题
查看>>
uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)
查看>>
Ruby对象模型总结
查看>>
排序算法--简单选择排序
查看>>
Google-Gson使用
查看>>
LeetCode[155]-Min Stack
查看>>