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 ...
随机推荐
- [CI]CodeIgniter系统流程
---------------------------------------------------------------------------------------------------- ...
- [Linux].deb软件包:wine-qq2013-longeneteam安装与卸载
--------------------------------------------------------------------------------------------- 首先切换到r ...
- c# vs2013部署项目
http://demo.netfoucs.com/shinepan/article/details/24865931
- c++之enum的好处与 define 的区别
转载自 https://blog.csdn.net/zhh464626057/article/details/41038933 什么时候需要用到enum呢?就是变量的数值在几个范围之间.red,blu ...
- 吴裕雄 31-MySQL 导出数据
MySQL中你可以使用SELECT...INTO OUTFILE语句来简单的导出数据到文本文件上. show global variables like '%secure%';SHOW VARIABL ...
- 处女座和小姐姐(三)-数位dp1.0
链接:https://ac.nowcoder.com/acm/contest/329/G来源:牛客网 题目描述 经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666! ...
- Halcon常用算子01
F1:Help F2:重置 F3:激活一行程序 F4:注销一行程序 F5:执行到stop()或程序结尾 F6:步执行(一步步调试) F10:添加或撤销断点 dev_open_window:打开图像窗口 ...
- Kubernetes 之上的架构应用
规划并运转一个兼顾可扩展性.可移植性和健壮性的运用是一件很有应战的事情,尤其是当体系杂乱度在不断增长时.运用或体系 本身的架构极大的影响着其运转办法.对环境的依靠性,以及与相关组件的耦合强弱.当运用在 ...
- with as 如何工作
with as 如何工作 with如何工作? Python对with的处理还是很机智滴.基本思想就是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法 紧跟wi ...
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...