1.创建windows服务应用
2.右键查看代码
3.写个计时器Timer using System.Timers;
如上图,按tab键快速操作 会自动创建一个委托
改为下边的方式,打印日志来记录服务运行
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; using System.Timers; namespace MyFirstWindowsService { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { WriteRunLog("服务开始了!!!"); Timer timer = new Timer(); timer.Interval = 1000; timer.Elapsed += Timer_Elapsed; timer.Start(); } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { WriteRunLog("当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss")); } protected override void OnStop() { WriteRunLog("服务结束了!!!"); } /// <summary> /// 记录运行日志 /// </summary> /// <param name="writeMsg"></param> public void WriteRunLog(string writeMsg) { FIle_Common file = new FIle_Common(); file.CreateDire(@"F:ServiceLog"); using (FileStream fs = new FileStream(@"F:ServiceLog" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write)) { StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("mcWindowsService:" + writeMsg + " Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " "); m_streamWriter.Flush(); m_streamWriter.Close(); fs.Close(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; namespace MyFirstWindowsService { public class FIle_Common { /// <summary> /// 创建文件夹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void CreateDire(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } /// <summary> /// 删除文件夹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void DeleteDire(string path) { if (Directory.Exists(path)) { Directory.Delete(path); } } /// <summary> /// 删除文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void DeleteDireOne(string path) { if (File.Exists(path)) { File.Delete(path); } } } }
4.右键添加安装程序
5.我用的VS2019 .net4.8 此时安装组件已经写好了,但是设计图里不显示,下边附上解决方案
6.解决方法:在这个类上边 using System.ServiceProcess;
此时 两个安装组件都显示出来了
7.设置服务安装属性
Description:对服务的说明
DisplayName:向用户标识服务的友好名称
ServiceName:表示在系统服务中的名称
StartType:启动服务的方式和时间,如果为Manual则手动启动,默认停止,如果为Automatic为自动启动
Accout:账户类型,LocalSystem本地系统服务
此时,Windows Service就编写完了
8.安装Windows Service
生成解决方案 进入inDebug 下 添加两个批处理文件来安装 卸载服务
statr.bat MyFirstWindowsService.exe是Debug下生成的.exe文件 MyFirstWindowsService是上边设置的ServiceName
%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe %~dp0MyFirstWindowsService.exe Net Start MyFirstWindowsService sc config MyFirstWindowsService start= auto pause
stop.bat
%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u %~dp0MyFirstWindowsService.exe pause
运行statr.bat
右键 我的电脑 管理
若要卸载该服务,运行stop.bat