C#Windows Service服務程序的安裝/卸載、啓動/中止 桌面客戶端管理程序設計

C#Windows Service服務程序的安裝/卸載、啓動/中止 桌面客戶端管理程序設計

關於Windows Service程序的安裝與卸載若是每次使用命令行操做,那簡直要奔潰了,太麻煩並且還容易出錯html

那麼若是你只是用一次就不用了那麼命令行業無所謂編程

關於Windows Service程序的建立能夠參考上一篇編程語言

C#Windows Service程序的建立安裝與卸載

1、命令行安裝與卸載

安裝服務:post

installutil.exe filename測試

卸載服務:
installutil.exe /u filenameui

安裝服務程序this

由於Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目錄下,須要經過cmd命令 "cd" 切換目錄。v4.0.30319是編譯該Windows Service程序的版本(本身選擇對應的版本)url

2、開發環境

操做系統:Windows7x64 sp1 專業版spa

開發環境:Visual studio 2013操作系統

編程語言:C#

.NET版本: .NET Frmework 4.0

3、新建一個客戶端程序進行安裝/卸載、啓動/中止

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

相關文章
相關標籤/搜索