標籤:top 啟動方式 override system pre conf 名稱 ref base
環境:vs2013,win7
參考:
Windows服務的快速搭建與調試(C#圖解)
.Net實現Windows服務安裝完成後自動啟動的兩種方法
C# 產生windows 服務打包程式
1、建立項目Windows Service項目。
2、服務安裝後自動啟動。
右鍵添加安裝程式
要設定服務的一些屬性。設定運行服務的帳號類型為本地賬戶。
設定服務名稱,設定啟動方式為自動啟動。
注意:Display Name:是設定看到的服務名稱。在電腦》管理》服務,看到的是這個。
Service Name :是設定實際的服務名稱。在Window 工作管理員裡面看到的是這個。
在工作管理員進程裡看到的是Windows Service項目的名稱。
在ProjectInstaller類中,覆蓋父類Commit方法
代碼如下:注意名稱,此名稱要與Windows Service設定的名稱一樣,在serviceInstaller1右鍵屬性裡面設定
1 [RunInstaller(true)] 2 public partial class ProjectInstaller : System.Configuration.Install.Installer 3 { 4 public ProjectInstaller() 5 { 6 InitializeComponent(); 7 } 8 9 public override void Commit(IDictionary savedState)10 {11 base.Commit(savedState);12 ServiceController sc = new ServiceController("Service1");13 if (sc.Status.Equals(ServiceControllerStatus.Stopped))14 {15 sc.Start();16 }17 }18 }
3、打包為exe檔案。
vs2013預設沒有打包項目,需要下載安裝。
Microsoft Visual Studio 2013 Installer Projects
安裝完後,解決方案右鍵添加新項目,Setup Project
Add ,項目輸出
選擇你的Windows service項目
在打包項目右鍵,view,自訂動作
每個都添加 自訂動作
最終效果
最後,就可以產生安裝包了,在bin目錄下Debug中
安裝完成後就可看到服務了
Windows Serivce服務實現過程和打包安裝