博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows服务安装、卸载、启动和关闭的管理器
阅读量:6979 次
发布时间:2019-06-27

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

      最近在重构公司的系统,把一些需要独立执行、并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了。但是每次都要建立Windows服务项目,编写服务代码,建立服务的安装程序,然后还要通过InstallUtil.exe这个命令来安装Windows服务,如果要是想卸载也要使用这个命令,只是在InstallUtil.exe这个命令后增加一个参数/u,表示卸载,我相信大家都对这个很熟悉了,我就不多说了。

      我为了不想使用这个命令来安装和卸载Windows服务,我就自己写了一个工具类,已经完全经过了单元测试,完全靠谱,大家可以放心使用。话不多说,直接上代码,代码有注释,其实也不是很难,相信大家都能看的懂。

1 using System;  2 using System.Collections;  3 using System.Collections.Generic;  4 using System.Configuration.Install;  5 using System.IO;  6 using System.Linq;  7 using System.ServiceProcess;  8 using System.Text;  9 using System.Threading.Tasks; 10  11 namespace Enterprise.Framework.Utils 12 { 13     ///  14     /// Windows服务实例的管理器,可以安装、启动、关停和卸载Windows服务实例 15     ///  16     public static class WindowsServiceInstanceManager 17     { 18         ///  19         /// 判断指定的名称的Windows服务是否存在 20         ///  21         /// Windows服务的名称 22         /// 
返回布尔值,true表示指定名称的Windows服务存在,false表示指定名称的Windows服务不存在
23 public static bool IsServiceExisted(string serviceName) 24 { 25 ServiceController[] services = ServiceController.GetServices(); 26 foreach (ServiceController controller in services) 27 { 28 if (string.Compare(controller.ServiceName, serviceName, true) == 0) 29 { 30 return true; 31 } 32 } 33 return false; 34 } 35 36 /// 37 /// 安装Windows服务,如果存在同名的服务,也认为安装时成功的 38 /// 39 /// 要安装的Windows服务的文件的地址 40 /// 要安装的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务存在,就不需要安装 41 ///
返回布尔值,true表示安装Windows服务成功,false表示安装Windows服务失败
42 public static bool InstallService(string serviceFilePath, string serviceName) 43 { 44 if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath)) 45 { 46 return false; 47 } 48 if (!File.Exists(serviceFilePath)) 49 { 50 return false; 51 } 52 if (this.IsServiceExisted(serviceName)) 53 { 54 return true; 55 } 56 using (AssemblyInstaller installer = new AssemblyInstaller()) 57 { 58 try 59 { 60 installer.UseNewContext = true; 61 installer.Path = serviceFilePath; 62 IDictionary savedState = new Hashtable(); 63 installer.Install(savedState); 64 installer.Commit(savedState); 65 return true; 66 } 67 catch (Exception) 68 { 69 throw; 70 } 71 } 72 } 73 74 /// 75 /// 卸载Windows服务,如果该名称的Windows服务不存在,也认识卸载失败 76 /// 77 /// 要卸载的Windows服务文件的地址 78 /// 要卸载的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务不存在,就不需要卸载 79 ///
返回布尔值,true表示卸载Windows服务成功,false表示卸载Windows服务失败
80 public static bool UninstallService(string serviceFilePath, string serviceName) 81 { 82 if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath)) 83 { 84 return false; 85 } 86 if (!File.Exists(serviceFilePath)) 87 { 88 return false; 89 } 90 if (!IsServiceExisted(serviceName)) 91 { 92 return false; 93 } 94 using (AssemblyInstaller installer = new AssemblyInstaller()) 95 { 96 try 97 { 98 installer.UseNewContext = true; 99 installer.Path = serviceFilePath;100 installer.Uninstall(null);101 return true;102 }103 catch (Exception)104 {105 throw;106 }107 }108 }109 110 /// 111 /// 启动Windows服务112 /// 113 /// 要启动的Windows服务的名称114 ///
返回布尔值,true表示启动Windows服务成功,false表示启动Windows服务失败
115 public static bool StartService(string serviceName)116 {117 if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))118 {119 return false;120 }121 using (ServiceController control = new ServiceController(serviceName))122 {123 try124 {125 if (control.Status == ServiceControllerStatus.Stopped)126 {127 control.Start();128 }129 return true;130 }131 catch (Exception)132 {133 throw;134 }135 }136 }137 138 /// 139 /// 关停Windows服务140 /// 141 /// 要关停Windows服务的名称142 ///
返回布尔值,true表示关停Windows服务成功,false表示关停Windows服务失败
143 public static bool StopService(string serviceName)144 {145 if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))146 {147 return false;148 }149 using (ServiceController control = new ServiceController(serviceName))150 {151 try152 {153 if (control.Status == ServiceControllerStatus.Running)154 {155 control.Stop();156 }157 return true;158 }159 catch (Exception)160 {161 throw;162 }163 }164 }165 }166 }

      好了,这就是今天自己的一点小作品,每天进步一点点,努力坚持。不忘初心,继续努力吧,欢迎大家前来讨论。

转载于:https://www.cnblogs.com/PatrickLiu/p/9402532.html

你可能感兴趣的文章
macbook中的一些快捷功能
查看>>
将CISCO 1242AG转换成胖AP
查看>>
图文并茂讲解VMware三种网络模式
查看>>
Ubuntu14.04搭建LAMP
查看>>
思科4506E交换机系统升级那点事!
查看>>
linux-mysql
查看>>
如何在Exchange Server 2003中重置提供OWA、EAS和OMA服务所需的默认虚拟目录
查看>>
GIT分布式版本控制系统使用教程
查看>>
1、Nginx安装和配置文件
查看>>
Centos网络管理(二)-IP与子网掩码计算
查看>>
网媒亟待建立广告价值衡量体系
查看>>
mysql-5.7.16一键安装/配置优化
查看>>
SSL握手中的个别细节
查看>>
从Linux 2.6.8内核的一个TSO/NAT bug引出的网络问题排查观点(附一个skb的优化点)
查看>>
SpringBoot 获取当前登录用户IP
查看>>
sed用法
查看>>
Windows Phone 应用发布技巧汇总
查看>>
centos6.0下安装FTP客户端命令
查看>>
【No.7 C++对象的构造与析构时间】
查看>>
Zabbix如何监控Windows机器
查看>>