C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计

C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计

关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出错

那么如果你只是用一次就不用了那么命令行业无所谓

关于Windows Service程序的创建可以参考上一篇

C#Windows Service程序的创建安装与卸载

一、命令行安装与卸载

安装服务:

installutil.exe filename

卸载服务:
installutil.exe /u filename

安装服务程序

因为Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目录下,需要通过cmd命令 "cd" 切换目录。v4.0.30319是编译该Windows Service程序的版本(自己选择对应的版本)

二、开发环境

操作系统:Windows7x64 sp1 专业版

开发环境:Visual studio 2013

编程语言:C#

.NET版本: .NET Frmework 4.0

三、新建一个客户端程序进行安装/卸载、启动/停止

1.新建一个WinForm窗体应用程序起名为Windows Service Client

2.添加四个按钮分别为安装服务/卸载服务、启动服务/停止服务

3.引入俩个命名空间引用“System.ServiceProcess”及“System.Configuration.Install”

4.编写代码如下

  1         string serviceFilePath = Environment.CurrentDirectory + "\\WindowsServiceDemo.exe";
  2         //string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe";
  3         string serviceName = "ServiceDemo";
  4 
  5         public Form1()
  6         {
  7             InitializeComponent();
  8         }
  9 
 10         /// <summary>
 11         /// 安装服务
 12         /// </summary>
 13         /// <param name="sender"></param>
 14         /// <param name="e"></param>
 15         private void button1_Click(object sender, EventArgs e)
 16         {
 17             if (this.IsServiceExisted(serviceName))
 18             {
 19                 this.UninstallService(serviceName);
 20             } 
 21             this.InstallService(serviceFilePath);
 22         }
 23 
 24         /// <summary>
 25         /// 卸载服务
 26         /// </summary>
 27         /// <param name="sender"></param>
 28         /// <param name="e"></param>
 29         private void button2_Click(object sender, EventArgs e)
 30         {
 31             if (this.IsServiceExisted(serviceName))
 32             {
 33                 this.ServiceStop(serviceName);
 34                 this.UninstallService(serviceFilePath);
 35             }
 36         }
 37 
 38         /// <summary>
 39         /// 启动服务程序
 40         /// </summary>
 41         /// <param name="sender"></param>
 42         /// <param name="e"></param>
 43         private void button3_Click(object sender, EventArgs e)
 44         {
 45             if (this.IsServiceExisted(serviceName))
 46             {
 47                 this.ServiceStart(serviceName);
 48             } 
 49         }
 50 
 51         /// <summary>
 52         /// 停止服务程序
 53         /// </summary>
 54         /// <param name="sender"></param>
 55         /// <param name="e"></param>
 56         private void button4_Click(object sender, EventArgs e)
 57         {
 58             if (this.IsServiceExisted(serviceName))
 59             {
 60                 this.ServiceStop(serviceName);
 61             } 
 62         }
 63 
 64         /// <summary>
 65         /// 判断服务是否存在
 66         /// </summary>
 67         /// <param name="serviceName"></param>
 68         /// <returns></returns>
 69         private bool IsServiceExisted(string serviceName)
 70         {
 71             ServiceController[] services = ServiceController.GetServices();
 72             foreach (ServiceController sc in services)
 73             {
 74                 if (sc.ServiceName.ToLower() == serviceName.ToLower())
 75                 {
 76                     return true;
 77                 }
 78             }
 79             return false;
 80         }
 81 
 82         /// <summary>
 83         /// 安装服务
 84         /// </summary>
 85         /// <param name="serviceFilePath"></param>
 86 
 87         private void InstallService(string serviceFilePath)
 88         {
 89             using (AssemblyInstaller installer = new AssemblyInstaller())
 90             {
 91                 installer.UseNewContext = true;
 92                 installer.Path = serviceFilePath;
 93                 IDictionary savedState = new Hashtable();
 94                 installer.Install(savedState);
 95                 installer.Commit(savedState);
 96             }
 97         }
 98 
 99         /// <summary>
100         /// 卸载服务
101         /// </summary>
102         /// <param name="serviceFilePath"></param>
103         private void UninstallService(string serviceFilePath)
104         {
105             using (AssemblyInstaller installer = new AssemblyInstaller())
106             {
107                 installer.UseNewContext = true;
108                 installer.Path = serviceFilePath;
109                 installer.Uninstall(null);
110             }
111         }
112         /// <summary>
113         /// 启动服务
114         /// </summary>
115         /// <param name="serviceName"></param>
116         private void ServiceStart(string serviceName)
117         {
118             using (ServiceController control = new ServiceController(serviceName))
119             {
120                 if (control.Status == ServiceControllerStatus.Stopped)
121                 {
122                     control.Start();
123                 }
124             }
125         }
126 
127         /// <summary>
128         /// 停止服务
129         /// </summary>
130         /// <param name="serviceName"></param>
131         private void ServiceStop(string serviceName)
132         {
133             using (ServiceController control = new ServiceController(serviceName))
134             {
135                 if (control.Status == ServiceControllerStatus.Running)
136                 {
137                     control.Stop();
138                 }
139             }
140         }

5.引入WindowsServerDemo程序到本项目中便于安装等

6.由于需要安装服务,故需要使用UAC中Administrator的权限,鼠标右击项目“WindowsServiceClient”,在弹出的上下文菜单中选择“添加”->“新建项”,在弹出的选择窗体中选择“应用程序清单文件”并单击确定,如下图所示:

7.打开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改为<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

1         <!--修改前
2         <requestedExecutionLevel level="asInvoker" uiAccess="false" />
3         -->
4         <!--修改后-->
5         <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

8.启动程序记住(Visual studio 2013也得用管理员身份运行),顺便开启计算机->管理-->服务来查看

分别单击测试安装服务,启动服务,停止服务,卸载服务,分别查看服务列表

服务列表

源代码工程文件下载

 参考博客:https://www.cnblogs.com/mq0036/p/7875864.html

posted @ 2018-05-19 17:24  JiYF  阅读(4723)  评论(1编辑  收藏  举报