1、建立windows服務工程MyService。
2、完成MyService類中的OnStart和OnStop方法,實現服務的啟動與停止。
註:建議該部分處理內容放在單獨的工程中,以後調試windows服務時,可以直接允許那個獨立工程而不用通過windows服務。
例如獨立對象使用:public static readonly ServiceProcesser ProcesserInstance = newServcieProcesser.ServiceProcesser();調用時使用:ServcieProcesser.ServiceProcesser.ProcesserInstance.OnStart();
3、添加安裝程式類,在項目上右鍵->添加->建立項->模板中選擇“安裝程式類”:MyServiceInstaller.
4、在建構函式中增加以下內容:
// 建立ServiceProcessInstaller對象和ServiceInstaller對象
System.ServiceProcess.ServiceProcessInstaller spInstaller =
new System.ServiceProcess.ServiceProcessInstaller();
System.ServiceProcess.ServiceInstaller sInstaller =
new System.ServiceProcess.ServiceInstaller();
// 設定ServiceProcessInstaller對象的帳號、使用者名稱和密碼等資訊
spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
//this.spInstaller.Username = null;
//this.spInstaller.Password = null;
// 設定服務名稱
sInstaller.ServiceName = "MyService";
sInstaller.Description = "***服務名稱";
// 設定服務的啟動方式
sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
Installers.AddRange(
new System.Configuration.Install.Installer[] { spInstaller, sInstaller });
5、產生工程,完成服務工程。
6、通過批處理安裝:bat檔案中寫如下內容。
installutil FaisEtuService.exe
通過批處理卸載:bat檔案中:
installutil /u FaisEtuService.exe
【注】:InstallUtil.exe,InstallUtilLib.dll