一、新建項目 選中windows服務windows
二、添加安裝程序測試
三、修改安裝代碼this
ServiceProcessInstaller processInstall; ServiceInstaller serviceInstall; public ProjectInstaller() { this.processInstall = new ServiceProcessInstaller(); this.serviceInstall = new ServiceInstaller(); processInstall.Account = ServiceAccount.LocalSystem; this.serviceInstall.ServiceName = "ABC_TaskService"; this.serviceInstall.Description = "ABC_Description"; this.Installers.Add(this.serviceInstall); this.Installers.Add(this.processInstall); }
四、從新生成項目spa
五、新建bat文件(安裝服務.bat)blog
@ECHO OFF REM The following directory is for .NET1.1 set DOTNETFX=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319 set PATH=%PATH%;%DOTNETFX% cd\ cd "G:\WindowsServiceTest\WindowsServiceTest\bin\Debug" echo 正在安裝 測試服務 echo --------------------------------------------------- InstallUtil /i WindowsServiceTest.exe sc config "ABC_TaskService" start= auto Net Start "ABC_TaskService" echo --------------------------------------------------- pause
注意:ip
一、cd "G:\WindowsServiceTest\WindowsServiceTest\bin\Debug" 路徑爲項目的路徑get
二、InstallUtil /i WindowsServiceTest.exe 其中WindowsServiceTest爲項目從新生成的文件名稱it
三、sc config "ABC_TaskService" start= auto 其中ABC_TaskService爲第三步爲服務起的名稱io
四、執行bat文件時必定要在管理員:命令提示符中操做 不然會錯(沒法打開計數機.上的服務控制管理器,拒絕訪問等錯誤)class
六、新建bat文件(卸載服務.bat)
@ECHO OFF REM The following directory is for .NET1.1 set DOTNETFX=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319 set PATH=%PATH%;%DOTNETFX% cd\ cd "G:\WindowsServiceTest\WindowsServiceTest\bin\Debug" echo 正在卸載 測試服務 echo --------------------------------------------------- InstallUtil /U WindowsServiceTest.exe echo --------------------------------------------------- pause