1.新建一个windows服务
2.添加安装程序
3.将serviceinstaller的servicename命名以及starttype设置为auto方式
4.将serviceprocessinstaller的account账户设置为本地系统
5.在安装程序的后台代码中添加一个commited事件,用于启动服务,代码如下:
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("AppointmentService");
controller.Start();
}
这样在安装服务后就会自动启动服务,每次电脑关闭后重新开启都直接自动开启服务不用每次都手动去开启
6.编写安装和卸载服务的命令bat文件,如下:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe WindowsAppointmentService.exe 安装服务
C:\Windows\Micr