1.新建windows服務項目
2.編輯業務代碼
我這里只寫2句記錄文本的測試代碼
1 using System; 2 using System.IO; 3 using System.ServiceProcess; 4 5 namespace WindowsService 6 { 7 public partial class Service : ServiceBase 8 { 9 public Service() 10 { 11 InitializeComponent(); 12 } 13 14 protected override void OnStart(string[] args) 15 { 16 System.IO.File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), string.Format("{0}我在開始了", DateTime.Now)); 17 } 18 19 protected override void OnStop() 20 { 21 System.IO.File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), string.Format("{0}我在停止了", DateTime.Now)); 22 } 23 } 24 }
3.添加安裝程序
4.設置ProjectInstaller屬性
這2個按照圖上面設置即可
下面分別導入bat文化
InstallUtil.bat(安裝)
WindowsService.exe 程序名稱
TestService 上面設置的服務名稱 ServiceName
InstallUtil WindowsService.exe
net start TestService
pause
startService.bat(啟動服務)
net start TestService
pause
stopService.bat(停止服務)
net stop TestService
pause
UnIntall.bat(卸載)
installutil /u WindowsService.exe
pause
把這個4個文件放在根目錄下面設置始終復制即可
還有一個文件InstallUtil.exe 也需要設置始終復制
做完這些操作然后生成一些程序 到bin目錄雙擊InstallUtil.bat安裝即可
下面給是demo