[原]创建windows服务


1、新建windows服务工程MyService。
2、完成MyService类中的OnStart和OnStop方法,实现服务的启动与停止。
注:建议该部分处理内容放在单独的工程中,以后调试windows服务时,可以直接允许那个独立工程而不用通过windows服务。
例如独立对象使用:public static readonly ServiceProcesser ProcesserInstance = newServcieProcesser.ServiceProcesser();调用时使用:ServcieProcesser.ServiceProcesser.ProcesserInstance.OnStart();

3、添加安装程序类,在项目上右键->添加->新建项->模板中选择“安装程序类”:MyServiceInstaller.
4、在构造函数中增加以下内容:

            // 创建ServiceProcessInstaller对象和ServiceInstaller对象
            System.ServiceProcess.ServiceProcessInstaller spInstaller =
                new System.ServiceProcess.ServiceProcessInstaller();
            System.ServiceProcess.ServiceInstaller sInstaller =
                new System.ServiceProcess.ServiceInstaller();

            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
            spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            //this.spInstaller.Username = null;
            //this.spInstaller.Password = null;

            // 设定服务名称
            sInstaller.ServiceName = "MyService";
            sInstaller.Description = "***服务名称";

            // 设定服务的启动方式
            sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            Installers.AddRange(
                new System.Configuration.Install.Installer[] { spInstaller, sInstaller });
5、生成工程,完成服务工程。
6、通过批处理安装:bat文件中写如下内容。
installutil FaisEtuService.exe
通过批处理卸载:bat文件中:
installutil /u FaisEtuService.exe
【注】:InstallUtil.exe,InstallUtilLib.dll

原文地址:https://www.cnblogs.com/xinyuxin912/p/1428856.html