将控制台程序改成windows服务

来源:互联网 发布:淘宝药品货到付款 编辑:程序博客网 时间:2024/06/05 23:29

1、在解决方案里添加windows服务,之后在Service1.cs[设计]页右键添加安装程序,在Service1.cs[代码]页的OnStart函数里编写程序启动时的代码

2、在ProjectInstaller.cs设置服务启动的部分属性,如:

            this.serviceInstaller1.ServiceName = "SuidaoWindowsService";//设置serviceInstaller1组件的名字。
            this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Manual;//手动启动运行方式。
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.serviceInstaller1.Description = "子站服务程序";

3、重新编译项目,将windows服务安装工具(install.bat、InstallUtil.exe、uninstall.bat--此工具在我上传的资源里有,名字为windows服务安装工具)放在启动项目的bin\Debug目录下,并编辑install.bat中的exe程序名字,保存后双击运行,安装完成后可在控制面板--管理工具--服务中找到。

4、windows服务卸载   在运行里cmd中输入   sc  delete 服务名字

5、windows服务调试   在服务启动过程中附加到进程调试