windows服务部署

时间:2023-03-10 00:47:48
windows服务部署

1.新建windows服务项目

windows服务部署

2.编辑业务代码

我这里只写2句记录文本的测试代码

 using System;
using System.IO;
using System.ServiceProcess; namespace WindowsService
{
public partial class Service : ServiceBase
{
public Service()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
System.IO.File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), string.Format("{0}我在开始了", DateTime.Now));
} protected override void OnStop()
{
System.IO.File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), string.Format("{0}我在停止了", DateTime.Now));
}
}
}

3.添加安装程序

windows服务部署

4.设置ProjectInstaller属性

windows服务部署

windows服务部署

windows服务部署

这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