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