1、新建项目 选中windows服务

2、添加安装程序

3、修改安装代码

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);
}
4、重新生成项目
5、新建bat文件(安装服务.bat)
@ECHO OFF REM The following directory is for .NET1.1 set DOTNETFX=%SystemRoot%Microsoft.NETFrameworkv4.0.30319 set PATH=%PATH%;%DOTNETFX% cd cd "G:WindowsServiceTestWindowsServiceTestinDebug" echo 正在安装 测试服务 echo --------------------------------------------------- InstallUtil /i WindowsServiceTest.exe sc config "ABC_TaskService" start= auto Net Start "ABC_TaskService" echo --------------------------------------------------- pause
注意:
1、cd "G:WindowsServiceTestWindowsServiceTestinDebug" 路径为项目的路径
2、InstallUtil /i WindowsServiceTest.exe 其中WindowsServiceTest为项目重新生成的文件名称
3、sc config "ABC_TaskService" start= auto 其中ABC_TaskService为第三步为服务起的名称
4、执行bat文件时一定要在管理员:命令提示符中操作 否则会错(无法打开计数机.上的服务控制管理器,拒绝访问等错误)


6、新建bat文件(卸载服务.bat)
@ECHO OFF REM The following directory is for .NET1.1 set DOTNETFX=%SystemRoot%Microsoft.NETFrameworkv4.0.30319 set PATH=%PATH%;%DOTNETFX% cd cd "G:WindowsServiceTestWindowsServiceTestinDebug" echo 正在卸载 测试服务 echo --------------------------------------------------- InstallUtil /U WindowsServiceTest.exe echo --------------------------------------------------- pause
