windows服务部署

1.新建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));
        }
    }
}
View Code

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

转:http://www.cnblogs.com/liuxiaoji/p/4779024.html

原文地址:https://www.cnblogs.com/love201314/p/4779156.html