.NET服务安装、卸载、启动、停止、判断是否存在

简介: 一、安装服务:private void InstallService(IDictionary stateSaver, string filepath){try{System.

一、安装服务:
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服务的方法:

目录
相关文章
|
7月前
|
网络安全 开发工具 git
win10专业版安装.net Framework4.7.2出现问题解决办法
win10专业版安装.net Framework4.7.2出现问题解决办法
586 0
|
6月前
|
关系型数据库 MySQL 数据库
找不到请求的 .Net Framework Data Provider。可能没有安装
做的一个项目,框架为.net framework 数据库为mysql 出现如标题错误 检查是否安装mysql、是否安装mysql connector net 笔者是因为没有安装后者mysql connector net 下载地址: [mysql connector net](https://downloads.mysql.com/archives/c-net/ "mysql connector net") 笔者安装截图如下: ![请在此添加图片描述](https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com/c
60 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
129 0
|
2月前
|
安全 C# 开发者
.NET开源的一键自动化下载、安装、激活Microsoft Office利器
.NET开源的一键自动化下载、安装、激活Microsoft Office利器
|
3月前
|
监控 Ubuntu 安全
百度搜索:蓝易云【Ubuntu安装Net-tools和SSH】
请注意,这些命令假设您已经具有适当的网络连接,并具有sudo或root权限。根据您的具体环境和要求,可能需要进行其他配置和调整。确保在进行任何与网络和安全相关的操作之前,详细了解您的需求和网络环境,并采取适当的安全措施。
65 1
|
4月前
|
C# Windows
C#安装“Windows 窗体应用(.NET Framework)”
C#安装“Windows 窗体应用(.NET Framework)”
51 0
|
5月前
|
Oracle 关系型数据库 Linux
在CentOS 7 下安装.Net 框架
在CentOS 7 下安装.Net 框架
74 0
|
6月前
|
Windows
Windows安装vSphere Client报错:错误28173,安装程序未能启用Microsoft.NET Framew..
Windows安装vSphere Client报错:错误28173,安装程序未能启用Microsoft.NET Framew..
103 0
|
7月前
|
容器
.NET Core - 选项框架:服务组件集成配置的最佳实践
.NET Core - 选项框架:服务组件集成配置的最佳实践
|
7月前
.NET Core - 配置框架:让服务无缝适应各种环境
.NET Core - 配置框架:让服务无缝适应各种环境