Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制
本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件)、判断Service是否存在、获得Service状态及启动停止Service。
创建Windows Service项目并Build得到exe文件,如何创建 Service 可参考 创建windows service 并打包成安装文件。
一、 Windows服务的安装和卸载
安装和卸载服务可以使用 .NET 工具installutil.exe (eg:安装-> installutil xxx.exe 卸载-> installutil /u xxx.exe),使用ManagedInstallerClass可以实现安装卸载Windows 服务,ManagedInstallerClass 在System.Configuration.Install命名空间下。
实现如下:

1 ///<summary>
2 /// 使用Windows Service对应的exe文件 安装Service
3 /// 和 installutil xxx.exe 效果相同
4 ///</summary>
5 ///<param name="installFile">exe文件(包含路径)</param>
6 ///<returns>是否安装成功</returns>
7 public static bool InstallServie(string installFile)
8 {
9 string[] args = { installFile };
10 try
11 {
12 ManagedInstallerClass.InstallHelper(args);
13 return true;
14 }
15 catch
16 {
17 return false;
18 }
19 }
20
21 ///<summary>
22 /// 使用Windows Service对应的exe文件 卸载Service
23 /// 和 installutil /u xxx.exe 效果相同
24 ///</summary>
25 ///<param name="installFile">exe文件(包含路径)</param>
26 ///<returns>是否卸载成功</returns>
27 public static bool UninstallService(string installFile)
28 {
29 string[] args = { "/u", installFile };
30 try
31 {
32 // 根据文件获得服务名,假设exe文件名和服务名相同
33 string tmp = installFile;
34 if (tmp.IndexOf('\\') != -1)
35 {
36 tmp = tmp.Substring(tmp.LastIndexOf('\\') + 1);
37 }
38 string svcName = tmp.Substring(0, tmp.LastIndexOf('.'));
39 // 在卸载服务之前 要先停止windows服务
40 StopService(svcName);
41
42 ManagedInstallerClass.InstallHelper(args);
43 return true;
44 }
45 catch
46 {
47 return false;
48 }
49 }

注意: 服务删除前需要先停止服务,否则服务被标记为禁用,并无法删除。原因:系统删除服务时,首先是标记服务为“删除”,等待服务的所有引用完全断开后,服务才被完全删除。当服务引用未完全断开时,就删除服务,系统将服务锁死为“禁用”状态,并禁止其他操作,注意此时服务尚未完全删除。所以对已删除后立即重装的服务,需要完全释放与服务相关的所有引用,此时系统才真正完全删除服务,之后才能再次安装服务。如果出现服务禁用问题,检查代码,哪些地方有服务引用,同时检查是否其他程序还在引用,也要终止这些引用程序。
二、Windows服务的状态获取和控制
使用ServiceController来获取服务状态或对服务进行控制。
ServiceController 表示 Windows 服务并允许连接到正在运行或者已停止的服务、对其进行操作或获取有关它的信息。使用 ServiceController 类连接到现有服务并控制其行为。当创建 ServiceController 类的实例时,设置其属性,以便它与特定的 Windows 服务交互作用。然后可以使用此类来启动、停止和以其他方式操作该服务。创建实例后,必须为其设置两个属性来标识与其交互的服务:计算机名称和要控制的服务的名称。默认情况下,MachineName 设置为本地计算机,因此不需要更改它,除非想将该实例设置为指向另一台计算机。
服务可以处理的命令集取决于该服务的属性;例如,可以将服务的 CanStop 属性设置为 false。该设置使 Stop 命令在那个特定的服务上不可用;它禁用了必要的按钮,使您无法从 SCM 中停止服务。如果试图通过代码停止服务,系统将引发错误,并显示错误信息“未能停止 servicename”。
常用操作如下:
1. 获得Service对应的ServiceController实例

1 ///<summary>
2 /// 获得service对应的ServiceController对象
3 ///</summary>
4 ///<param name="serviceName">服务名</param>
5 ///<returns>ServiceController对象,若没有该服务,则返回null</returns>
6 public static ServiceController GetService(string serviceName)
7 {
8 ServiceController[] services = ServiceController.GetServices();
9 foreach (ServiceController s in services)
10 {
11 if (s.ServiceName == serviceName)
12 {
13 return s;
14 }
15 }
16 return null;
17 }

2. 检查指定的服务是否存在

1 ///<summary>
2 /// 检查指定的服务是否存在。
3 ///</summary>
4 ///<param name="serviceName">要查找的服务名字</param>
5 ///<returns>是否存在</returns>
6 public static bool ServiceExisted(string serviceName)
7 {
8 if (GetService(serviceName) == null)
9 {
10 return false;
11 }
12 else
13 {
14 return true;
15 }
16 }

3. 获得服务详细信息

1 ///<summary>
2 /// 获得Service的详细信息
3 ///</summary>
4 ///<param name="serviceName">服务名</param>
5 ///<returns>Service信息,保存在string中</returns>
6 public static string GetServiceInfo(string serviceName)
7 {
8 StringBuilder details = new StringBuilder();
9
10 ServiceController sc = GetService(serviceName);
11
12 if (sc == null)
13 {
14 return string.Format("{0} 不存在!", serviceName);
15 }
16
17 details.AppendLine(string.Format("服务标识的名称: {0}", sc.ServiceName));
18 details.AppendLine(string.Format("服务友好名称:{0}", sc.DisplayName));
19 details.AppendLine(string.Format("服务在启动后是否可以停止: {0}", sc.CanStop));
20 details.AppendLine(string.Format("服务所驻留的计算机的名称: {0}", sc.MachineName)); // "." 表示本地计算机
21 details.AppendLine(string.Format("服务类型: {0}", sc.ServiceType.ToString()));
22 details.AppendLine(string.Format("服务状态: {0}", sc.Status.ToString()));
23
24 // DependentServices 获取依赖于与此 ServiceController 实例关联的服务的服务集。
25 StringBuilder dependentServices = new StringBuilder();
26 foreach (ServiceController s in sc.DependentServices)
27 {
28 dependentServices.Append(s.ServiceName + ", ");
29 }
30 details.AppendLine(string.Format("依赖于与此 ServiceController 实例关联的服务的服务: {0}", dependentServices.ToString()));
31
32 // ServicesDependedOn 此服务所依赖的服务集。
33 StringBuilder serviceDependedOn = new StringBuilder();
34 foreach (ServiceController s in sc.ServicesDependedOn)
35 {
36 serviceDependedOn.Append(s.ServiceName + ", ");
37 }
38 details.AppendLine(string.Format("此服务所依赖的服务: {0}", serviceDependedOn.ToString()));
39
40 return details.ToString();
41 }

4. 启动服务

1 ///<summary>
2 /// 启动服务
3 ///</summary>
4 ///<param name="serviceName">服务名</param>
5 ///<returns>是否启动成功</returns>
6 public static bool StartService(string serviceName)
7 {
8 ServiceController sc = GetService(serviceName);
9
10 if (sc.Status != ServiceControllerStatus.Running)
11 {
12 try
13 {
14 sc.Start();
15 sc.WaitForStatus(ServiceControllerStatus.Running); // 等待服务达到指定状态
16 }
17 catch
18 {
19 return false;
20 }
21 }
22
23 return true;
24 }

5. 停止服务

1 ///<summary>
2 /// 停止服务
3 ///</summary>
4 ///<param name="serviceName">服务名</param>
5 ///<returns>是否停止服务成功,如果服务启动后不可以停止,则抛异常</returns>
6 public static bool StopService(string serviceName)
7 {
8 ServiceController sc = GetService(serviceName);
9
10 if (!sc.CanStop)
11 {
12 throw new Exception(string.Format("服务{0}启动后不可以停止.", serviceName));
13 }
14
15 if (sc.Status != ServiceControllerStatus.Stopped)
16 {
17 try
18 {
19 sc.Stop();
20 sc.WaitForStatus(ServiceControllerStatus.Stopped); // 等待服务达到指定状态
21 }
22 catch
23 {
24 return false;
25 }
26 }
27
28 return true;
29 }

Windows Service的安装卸载 和 Service控制的更多相关文章
- Windows Service的安装卸载 和 Service控制(转)
Windows Service的安装卸载 和 Service控制 原文地址:http://www.cnblogs.com/Peter-Zhang/archive/2011/10/15/2212663. ...
- Windows服务的安装卸载及错误查找
@echo off echo 清理原有服务项. . . %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil /U D:\abc\te ...
- window service 批处理安装/卸载命令
开启服务 @echo.服务启动...... @echo off @sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32sr ...
- WCF 下的windows服务的安装卸载
安装:启动vs2010(如果是win2008要以管理员来启动)命令:installutil demo.exe 卸载:先在服务里停止这个服务,然后启动vs2010(如果是win2008要以管理员来启动) ...
- C# Windows服务创建安装卸载
一.创建Windows服务 使用VS创建一个新的windows服务应用程序 创建完成之后 二.相关配置 修改Service1名称为StartService(可以不改,自行选择) 添加安装程序并修改配置 ...
- windows系统命令服务安装卸载
安装: sc create PDW.CHM.WebAPI binPath= "%~dp0PDW.CHM.WebAPI.exe" start= autosc start PDW.CH ...
- 通过批处理进行Windows服务的安装/卸载&启动/停止
安装服务 @echo off set checked=2 set PATHS=%~sdp0 echo 按任意键执行安装……? pause>nul if %checked% EQU 2 ( %PA ...
- Windows Service 开发,安装与调试
Visual Studio.net 2010 Windows Service 开发,安装与调试 本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Servic ...
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...
随机推荐
- Linux编程---I/O部分
非常多函数都能够在网上找到,也比較基础,所以原型仅仅给出了函数名.详细用到再man吧. 输入输出是个非常重要的一块内容.差点儿网络相关的东西基本都是靠底层IO调用来实现的. 好吧.还是先踏踏实实的介绍 ...
- Visual Studio 创建和使用dll的方法
DLL是一个包含可由多个程序同时使用的代码和数据的库. DLL文件就是把一些函数导出来,然后在新程序中进行复用的过程. 第一部分:使用Visual Studio 2010进行DLL的制作 生成方法一: ...
- linux常用命令系列—cp 复制文件与文件夹
原文地址:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2272&id=37363 指令名称:cp(copy)功能介绍 ...
- [置顶] Linux下文件和目录权限说明
在Linux下使用ls -l或者ll命令可以查看文件和文件夹的权限.结果显示类似于: drwxrwxrwx,这里分为四组,分别为文件类型,文件所有者的权限(读写执行),文件所有者所在组用户的权限(读写 ...
- USACO Runaround Numbers 模拟
根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字 模拟题~ Source code: /* ID: wushuai2 PROG: runround LANG: C ...
- BNU 4067 求圆并
好久没写过单组数据的题目了 QAQ 赤裸裸的模板题 #include <cstdio> #include <cstring> #include <iostream> ...
- yii_wiki_204_using-cjuidialog-to-edit-rows-in-a-cgridview(通过CJuiDialog在CGridView中修改行数据)
/*** Using CJuiDialog to edit rows in a CGridView http://www.yiiframework.com/wiki/204/using-cjuidia ...
- 基于visual Studio2013解决C语言竞赛题之0509杨辉三角
题目
- PHP - 多文件上传
<html> <head> <meta charset="utf-8"> <title>index_uploads</titl ...
- 嵌入式MCU开发群资源
STM32CubeMX是一款图形化软件设置工具,允许使用图形化向导来生成C初始化代码.它是未来开发stm32系列产品的主流软件,是ST公司的主动原创,可以减轻开发工作,时间和费用.STM32Cube ...