使用C#創建Windows服務
https://www.cnblogs.com/cncc/p/7170951.html
本文屬於原創,轉載請注明出處,謝謝!
一、開發環境
操作系統:Windows 10 X64
開發環境:VS2015
編程語言:C#
.NET版本:.NET Framework 4.0
目標平台:X86
二、創建Windows Service
1、新建一個Windows Service,並將項目名稱改為“MyWindowsService”,如下圖所示:
2、在解決方案資源管理器內將Service1.cs改為MyService1.cs后並點擊“查看代碼”圖標按鈕進入代碼編輯器界面,如下圖所示:
3、在代碼編輯器內如入以下代碼,如下所示:
using System; using System.ServiceProcess; using System.IO;namespace MyWindowsService
{
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}</span><span style="color: rgba(0, 0, 255, 1)">string</span> filePath = <span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">D:\MyServiceLog.txt</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span> OnStart(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args) { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (FileStream stream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileStream(filePath,FileMode.Append)) </span><span style="color: rgba(0, 0, 255, 1)">using</span> (StreamWriter writer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StreamWriter(stream)) { writer.WriteLine($</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DateTime.Now},服務啟動!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); } } </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnStop() { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (FileStream stream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileStream(filePath, FileMode.Append)) </span><span style="color: rgba(0, 0, 255, 1)">using</span> (StreamWriter writer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StreamWriter(stream)) { writer.WriteLine($</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DateTime.Now},服務停止!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); } } }
}
4、雙擊項目“MyWindowsService”進入“MyService”設計界面,在空白位置右擊鼠標彈出上下文菜單,選中“添加安裝程序”,如下圖所示:
5、此時軟件會生成兩個組件,分別為“serviceInstaller1”及“serviceProcessInstaller1”,如下圖所示:
6、點擊“serviceInstaller1”,在“屬性”窗體將ServiceName改為MyService,Description改為我的服務,StartType保持為Manual,如下圖所示:
7、點擊“serviceProcessInstaller1”,在“屬性”窗體將Account改為LocalSystem(服務屬性系統級別),如下圖所示:
8、鼠標右鍵點擊項目“MyWindowsService”,在彈出的上下文菜單中選擇“生成”按鈕,如下圖所示:
9、至此,Windows服務已經創建完畢。
三、創建安裝、啟動、停止、卸載服務的Windows窗體
1、在同一個解決方案里新建一個Windows Form項目,並命名為WindowsServiceClient,如下圖所示:
2、將該項目設置為啟動項目,並在窗體內添加四個按鈕,分別為安裝服務、啟動服務、停止服務及卸載服務,如下圖所示:
3、按下F7進入代碼編輯界面,引用“System.ServiceProcess”及“System.Configuration.Install”,並輸入如下代碼:
using System; using System.Collections; using System.Windows.Forms; using System.ServiceProcess; using System.Configuration.Install;namespace WindowsServiceClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serviceFilePath = $"{Application.StartupPath}\MyWindowsService.exe";
string serviceName = "MyService";</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">事件:安裝服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> button1_Click(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.IsServiceExisted(serviceName)) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.UninstallService(serviceName); </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.InstallService(serviceFilePath); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">事件:啟動服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> button2_Click(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.IsServiceExisted(serviceName)) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.ServiceStart(serviceName); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">事件:停止服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> button4_Click(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.IsServiceExisted(serviceName)) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.ServiceStop(serviceName); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">事件:卸載服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> button3_Click(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.IsServiceExisted(serviceName)) { </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.ServiceStop(serviceName); </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.UninstallService(serviceFilePath); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判斷服務是否存在</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> IsServiceExisted(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serviceName) { ServiceController[] services </span>=<span style="color: rgba(0, 0, 0, 1)"> ServiceController.GetServices(); </span><span style="color: rgba(0, 0, 255, 1)">foreach</span> (ServiceController sc <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> services) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (sc.ServiceName.ToLower() ==<span style="color: rgba(0, 0, 0, 1)"> serviceName.ToLower()) { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">安裝服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> InstallService(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serviceFilePath) { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (AssemblyInstaller installer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AssemblyInstaller()) { installer.UseNewContext </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; installer.Path </span>=<span style="color: rgba(0, 0, 0, 1)"> serviceFilePath; IDictionary savedState </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">卸載服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> UninstallService(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serviceFilePath) { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (AssemblyInstaller installer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AssemblyInstaller()) { installer.UseNewContext </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; installer.Path </span>=<span style="color: rgba(0, 0, 0, 1)"> serviceFilePath; installer.Uninstall(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">啟動服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> ServiceStart(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serviceName) { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (ServiceController control = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceController(serviceName)) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (control.Status ==<span style="color: rgba(0, 0, 0, 1)"> ServiceControllerStatus.Stopped) { control.Start(); } } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">停止服務</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> ServiceStop(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serviceName) { </span><span style="color: rgba(0, 0, 255, 1)">using</span> (ServiceController control = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceController(serviceName)) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (control.Status ==<span style="color: rgba(0, 0, 0, 1)"> ServiceControllerStatus.Running) { control.Stop(); } } } }
}
4、為了后續調試服務及安裝卸載服務的需要,將已生成的MyWindowsService.exe引用到本Windows窗體,如下圖所示:
5、由於需要安裝服務,故需要使用UAC中Administrator的權限,鼠標右擊項目“WindowsServiceClient”,在彈出的上下文菜單中選擇“添加”->“新建項”,在彈出的選擇窗體中選擇“應用程序清單文件”並單擊確定,如下圖所示:
6、打開該文件,並將<requestedExecutionLevel level="asInvoker" uiAccess="false" />改為<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,如下圖所示:
7、IDE啟動后,將會彈出如下所示的窗體(有的系統因UAC配置有可能不顯示),需要用管理員權限打開:
8、重新打開后,在IDE運行WindowsServiceClient項目;
9、使用WIN+R的方式打開運行窗體,並在窗體內輸入services.msc后打開服務,如下圖所示:
10、點擊窗體內的“安裝服務”按鈕,將會在服務中出現MyService,如下圖所示:
11、點擊“運行服務”按鈕,將啟動並運行服務,如下所示:
12、點擊“停止服務”按鈕,將會停止運行服務,如下圖所示:
13、點擊“卸載服務”按鈕,將會從服務中刪除MyService服務。
14、以上啟動及停止服務將會寫入D:\MyServiceLog.txt,內容如下所示:
源代碼下載:
補充:如何調試服務
1、要調試服務,其實很簡單,如需將服務附加進程到需要調試的項目里面即可,假如要調試剛才建的服務,現在OnStop事件里設置斷點,如下所示:
2、啟動“WindowsServiceClient”項目,在“調試”菜單中選擇“附件到進程”(服務必須事先安裝),如下所示:
3、找到“MyWindowsService.exe”,點擊“附加”按鈕,如下圖所示:
4、點擊“停止服務”按鈕,程序將會在設置斷點的地方中斷,如下圖所示: