Windows服务安装、卸载、启动和关闭的管理器
最近在重构公司的系统,把一些需要独立执行、并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了。但是每次都要建立Windows服务项目,编写服务代码,建立服务的安装程序,然后还要通过InstallUtil.exe这个命令来安装Windows服务,如果要是想卸载也要使用这个命令,只是在InstallUtil.exe这个命令后增加一个参数/u,表示卸载,我相信大家都对这个很熟悉了,我就不多说了。
我为了不想使用这个命令来安装和卸载Windows服务,我就自己写了一个工具类,已经完全经过了单元测试,完全靠谱,大家可以放心使用。话不多说,直接上代码,代码有注释,其实也不是很难,相信大家都能看的懂。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace Enterprise.Framework.Utils
{
/// <summary>
/// Windows服务实例的管理器,可以安装、启动、关停和卸载Windows服务实例
/// </summary>
public static class WindowsServiceInstanceManager
{
/// <summary>
/// 判断指定的名称的Windows服务是否存在
/// </summary>
/// <param name="serviceName">Windows服务的名称</param>
/// <returns>返回布尔值,true表示指定名称的Windows服务存在,false表示指定名称的Windows服务不存在</returns>
public static bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController controller in services)
{
if (string.Compare(controller.ServiceName, serviceName, true) == )
{
return true;
}
}
return false;
} /// <summary>
/// 安装Windows服务,如果存在同名的服务,也认为安装时成功的
/// </summary>
/// <param name="serviceFilePath">要安装的Windows服务的文件的地址</param>
/// <param name="serviceName">要安装的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务存在,就不需要安装</param>
/// <returns>返回布尔值,true表示安装Windows服务成功,false表示安装Windows服务失败</returns>
public static bool InstallService(string serviceFilePath, string serviceName)
{
if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath))
{
return false;
}
if (!File.Exists(serviceFilePath))
{
return false;
}
if (this.IsServiceExisted(serviceName))
{
return true;
}
using (AssemblyInstaller installer = new AssemblyInstaller())
{
try
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
return true;
}
catch (Exception)
{
throw;
}
}
} /// <summary>
/// 卸载Windows服务,如果该名称的Windows服务不存在,也认识卸载失败
/// </summary>
/// <param name="serviceFilePath">要卸载的Windows服务文件的地址</param>
/// <param name="serviceName">要卸载的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务不存在,就不需要卸载</param>
/// <returns>返回布尔值,true表示卸载Windows服务成功,false表示卸载Windows服务失败</returns>
public static bool UninstallService(string serviceFilePath, string serviceName)
{
if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath))
{
return false;
}
if (!File.Exists(serviceFilePath))
{
return false;
}
if (!IsServiceExisted(serviceName))
{
return false;
}
using (AssemblyInstaller installer = new AssemblyInstaller())
{
try
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
return true;
}
catch (Exception)
{
throw;
}
}
} /// <summary>
/// 启动Windows服务
/// </summary>
/// <param name="serviceName">要启动的Windows服务的名称</param>
/// <returns>返回布尔值,true表示启动Windows服务成功,false表示启动Windows服务失败</returns>
public static bool StartService(string serviceName)
{
if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))
{
return false;
}
using (ServiceController control = new ServiceController(serviceName))
{
try
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
return true;
}
catch (Exception)
{
throw;
}
}
} /// <summary>
/// 关停Windows服务
/// </summary>
/// <param name="serviceName">要关停Windows服务的名称</param>
/// <returns>返回布尔值,true表示关停Windows服务成功,false表示关停Windows服务失败</returns>
public static bool StopService(string serviceName)
{
if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))
{
return false;
}
using (ServiceController control = new ServiceController(serviceName))
{
try
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
return true;
}
catch (Exception)
{
throw;
}
}
}
}
}
好了,这就是今天自己的一点小作品,每天进步一点点,努力坚持。不忘初心,继续努力吧,欢迎大家前来讨论。
Windows服务安装、卸载、启动和关闭的管理器的更多相关文章
- C#编写的windows服务安装后启动提示“服务启动后又停止了”
使用C#编写的windows服务安装到服务器上行后进行启动时,总是提示“服务启动后又停止了”. 检查了服务逻辑是没问题,安装在开发本地也是正常,网上查了资料说是可能是服务没有注册,我检查了服务是正常注 ...
- C#版Windows服务安装卸载小工具-附源码
前言 在我们的工作中,经常遇到Windows服务的安装和卸载,在之前公司也普写过一个WinForm程序选择安装路径,这次再来个小巧灵活的控制台程序,不用再选择,只需放到需要安装服务的目录中运行就可以实 ...
- windows服务安装卸载
到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...
- 2.Windows服务-->安装卸载服务
1.使用vs组件“VS2012开发人员命令提示” 工具,进行安装卸载服务(必须以“管理员身份运行") 安装和卸载的时候选择合适的安装程序工具地址,例如: 安装服务:C:\Windows\Mi ...
- windows 服务 安装 删除 启动 停止
一.停止 sc stop 服务名 二.删除 sc delete 服务名 注意:有时删除不了,报什么“服务为删除标识” ,请将服务窗口关掉就好了. 三.创建 sc create XmlcSendServ ...
- Windows服务安装与卸载
Windows服务安装与卸载,使用到了InstallUtil.exe 安装: c: cd "C:\Windows\Microsoft.NET\Framework\v4.0.30319&quo ...
- C# windows服务安装及卸载
--C# windows服务安装及卸载 保存BAT文件 执行即可 @SET FrameworkDir=%WINDIR%\Microsoft.NET\Framework@SET Framework ...
- Windows 服务安装与卸载 (通过 Sc.exe)
1. 安装 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: sc create "Verity Platform De ...
- Windows 服务安装与卸载 (通过 installutil.exe)
1. 安装 安装 .NET Framework ; 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: C:\\Windows\\M ...
随机推荐
- 【原创】字典攻击教务处(BurpSuite使用)
0x00 本例使用Burp Suite跑字典爆破教务处登录. 使用账户名:yanjiushengdadui 本示例将结合说明Burp Suite的基本使用. 0x01 BurpSuite代理配置 浏览 ...
- 使用 Asp.Net Response.Write() 制作实时进度条
准备: 一个 StudyResponse.aspx 页面和 CodeBehind 文件. Web 页面中的内容如下: <%@ Page Language="C#" AutoE ...
- eclipse git 忽略文件
ps:git中只有.gitignore文件需要先加索引再提交,其它的都可以直接提交
- js固定底部菜单
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 函数练习以及 if else 三元运算
- 初始C语言中的指针(翁凯男神MOOC)
运算符 & ● scanf("%d",&i); ●获得变量的地址,它的操作数必须是变量 ● int i; printf("%x",& ...
- 微信小程序---模版
微信小程序用的是否娴熟,会灵活使用模版很重要. 新建一个template文件,做一个step模版. <template name="top"> <view cla ...
- linux下WIFI的AP搜索、连接方法
wpa_supplicant -Dwext -ieth1 -c/etc/wpa_supplicant.conf &wpa_cli save_configwpa_cli reconfigure ...
- mongodb副本集的从库永久性设置setSlaveOk
今天在生产环境下面搭了一个mongo的副本集,但开发那边要求副本集读写分离. 坑爹的是每次上副本集的时候都要设置db.getMongo().setSlaveOk()才能访问数据.感觉很是苦逼. 后来开 ...
- Linux下鼠标滚轮速度调整
安装imwheel 于home下创建.imwheelrc gedit ~/.imwheelrc 在.imwheelrc中粘贴以下内容 ".*" None, Up, Button4, ...