VS2017开发Windows服务
来源:互联网 发布:价值观不同的体现知乎 编辑:程序博客网 时间:2024/05/19 22:57
转载自:http://www.cnblogs.com/xujie/p/5695673.html
1、新建windows服务项目,我这里选择的是Framework4.0,没有选择高版本是为了防止在服务在一些低版本系统上无法正常运行。
2、添加Windows服务的安装程序。
在默认Service1设计器界面空白处点击右键->添加安装程序,系统会自动新建一个带有默认配置的安装程序类,如下图:
新建完安装程序后,需要给默认的serviceInstaller1和serviceProcessInstaller1做一些基本的属性设置。如下图:
以上工作完成,安装程序配置完毕。
注意:如果不给服务添加安装程序,后面是没法把服务安装至windows系统里的。
3、添加应用程序配置文件(如果有需要的话)。
如果项目有需要,一些应用程序的配置参数可以设置在此文件里(例如:数据库连接字符串)。
4、编写windows服务主代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.IO;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;namespace WinServiceTest{ public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent); timer.Interval = 5000;//每5秒执行一次 timer.Enabled = true; } public int count = 0; //定时执行事件 private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e) { //业务逻辑代码 EmailClass mail = new EmailClass(); mail.Email(count++); } protected override void OnStart(string[] args) { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n"); this.WriteLog("客户端数据同步服务:【服务启动】"); } protected override void OnStop() { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")+ "\n"); this.WriteLog("客户端数据同步服务:【服务停止】"); } protected override void OnShutdown() { this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n"); this.WriteLog("客户端数据同步服务:【计算机关闭】"); } #region 记录日志 /// <summary> /// 记录日志 /// </summary> /// <param name="msg"></param> private void WriteLog(string msg) { //string path = @"C:\log.txt"; //该日志文件会存在windows服务程序目录下 string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt"; FileInfo file = new FileInfo(path); if (!file.Exists) { FileStream fs; fs = File.Create(path); fs.Close(); } using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(DateTime.Now.ToString() + " " + msg); } } } #endregion }}
4、安装与卸载服务
readme里面的内容
请将【WinServiceTest】拷贝到D盘或C盘根目录;安装服务【管理员身份】运行【SC安装-发送邮件】即可;卸载服务【管理员身份】运行【SC卸载】即可;
SC安装-发送邮件:
@echo.请稍等,服务启动...... @echo off @sc create GX_To_EMAIL binPath= "D:\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe" DisplayName=每隔一段时间发送邮件的服务 start= auto@sc description GX_To_EMAIL 定时发送邮件@sc start GX_To_EMAIL@echo off @echo.启动完毕! @pause
SC卸载:
@echo.服务卸载...... @echo off @sc stop GX_To_EMAIL@sc delete GX_To_EMAIL@sc stop GX_To_EMAIL@echo off @echo.卸载完毕! @pause
阅读全文
1 0
- VS2017开发Windows服务
- 【OpenCV3.3】编译源码并搭建VS2017+Windows开发环境
- 【OpenCV3.3】编译源码并搭建VS2017+Windows开发环境
- vs2017 安卓开发
- VS2017开发activex
- 如何开发Windows服务
- windows服务开发
- .net Windows服务开发
- 如何开发Windows服务
- .net windows 服务开发
- C# 开发windows 服务
- c++开发windows服务
- WINDOWS 服务开发
- C#开发windows服务
- windows服务开发学习
- windows服务开发步骤
- VC Windows 服务开发
- WINDOWS服务开发
- 字符串的连接
- HBase之——Shell基础操作
- LeetCode 328. Odd Even Linked List
- 设置坐标轴长度和范围
- python的MySQL操作增删改查
- VS2017开发Windows服务
- java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
- 网络基础之信息的生成与传递
- 1043. 输出PATest(20)
- 存储空间分配问题.笔记
- 高级软件工程实验五
- 执行jmap -heap PID VMVersionMismatchException: Supported versions are 24.75-b04.
- Bzoj
- day76_maven02:maven拆分各层配置后,【pom.xml、Spring配置都按层拆分(dao/service/action)】,导致报错解决。