1. 程式人生 > >c#創建Windows service (Windows 服務)基礎教程

c#創建Windows service (Windows 服務)基礎教程

log 腳本 mage src installer als highlight syn project

轉自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html

1)創建Windows service項目

技術分享

技術分享

二、創建服務安裝程序

1)添加安裝程序

技術分享

技術分享

之後我們可以看到上圖,自動為我們創建了ProjectInstaller.cs以及2個安裝的組件。

2)修改安裝服務名

右鍵serviceInsraller1,選擇屬性,將ServiceName的值改為ServiceTest。

技術分享

3)修改安裝權限

右鍵serviceProcessInsraller1,選擇屬性,將Account的值改為LocalSystem。

技術分享

三、寫入服務代碼

1)打開ServiceTest代碼

右鍵ServiceTest,選擇查看代碼。

2)寫入Service邏輯

四、創建安裝腳本(在WindowsServiceTest.exe所在的目錄下創建)

技術分享

在項目中添加2個文件如下(必須是ANSI或者UTF-8無BOM格式):

1)安裝腳本Install.bat

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\huiyishi\WindowsServiceTest\WindowsServiceTest\bin\Debug\WindowsServiceTest.exe

2)卸載腳本Uninstall.bat

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u D:\huiyishi\WindowsServiceTest\WindowsServiceTest\bin\Debug\WindowsServiceTest.exe

c#創建Windows service (Windows 服務)基礎教程