• 大小: 42KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-22
  • 语言: 其他
  • 标签: WindowsServi  c#  服务  

资源简介

windowsService--实现文本的定时写数据操作,服务的开启与结束记录,定时写任务。

资源截图

代码片段和文件信息

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 WriteService
{
    public partial class MyWriteService : Servicebase
    {
        public MyWriteService()
        {
            InitializeComponent();
        }
        string filePath = @“D:\MyServiceLog.txt“;
        //定时器
        System.Timers.Timer tmBak = new System.Timers.Timer();
        //服务器启动时写日志、开启定时器
        protected override void onstart(string[] args)
        {
            using (FileStream stream = new FileStream(filePath FileMode.Append))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss“) +“服务启动!“);
            }
            //到时间的时候执行事件 
            tmBak.Interval = 2000;//10S执行一次
            tmBak.AutoReset = true;//执行一次 false,一直执行true 
            //是否执行System.Timers.Timer.Elapsed事件 
            tmBak.Enabled = true;
            tmBak.Start();
            tmBak.Elapsed += new System.Timers.ElapsedEventHandler(WriteLog);

        }

        protected void WriteLog(object source System.Timers.ElapsedEventArgs e)
        {
            using (FileStream stream = new FileStream(filePath FileMode.Append))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss“) + “执行定时任务写操作!“);
            }
        }

        //服务停止时写日志
        protected override void onstop()
        {
            using (FileStream stream = new FileStream(filePath FileMode.Append))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine(DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss“) + “服务停止!“);
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件          3  2019-01-23 08:52  WriteService\MyServiceLog.txt

     文件        187  2019-01-22 17:40  WriteService\WriteService\App.config

     文件       7680  2019-01-22 17:48  WriteService\WriteService\bin\Debug\WriteService.exe

     文件        187  2019-01-22 17:40  WriteService\WriteService\bin\Debug\WriteService.exe.config

     文件        717  2019-01-22 17:44  WriteService\WriteService\bin\Debug\WriteService.InstallLog

     文件       7466  2019-01-22 17:44  WriteService\WriteService\bin\Debug\WriteService.InstallState

     文件      19968  2019-01-22 17:48  WriteService\WriteService\bin\Debug\WriteService.pdb

     文件      23168  2019-01-23 08:28  WriteService\WriteService\bin\Debug\WriteService.vshost.exe

     文件        187  2019-01-22 17:40  WriteService\WriteService\bin\Debug\WriteService.vshost.exe.config

     文件        490  2013-03-18 17:00  WriteService\WriteService\bin\Debug\WriteService.vshost.exe.manifest

     文件       2163  2019-01-22 17:47  WriteService\WriteService\MyWriteService.cs

     文件       1093  2019-01-22 17:42  WriteService\WriteService\MyWriteService.Designer.cs

     文件       1624  2019-01-23 08:34  WriteService\WriteService\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6871  2019-01-22 17:42  WriteService\WriteService\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2019-01-22 17:40  WriteService\WriteService\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2019-01-22 17:40  WriteService\WriteService\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2019-01-22 17:40  WriteService\WriteService\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        455  2019-01-23 08:28  WriteService\WriteService\obj\Debug\WriteService.csproj.FileListAbsolute.txt

     文件        915  2019-01-22 17:47  WriteService\WriteService\obj\Debug\WriteService.csproj.GenerateResource.Cache

     文件       7680  2019-01-22 17:48  WriteService\WriteService\obj\Debug\WriteService.exe

     文件      19968  2019-01-22 17:48  WriteService\WriteService\obj\Debug\WriteService.pdb

     文件        180  2019-01-22 17:47  WriteService\WriteService\obj\Debug\WriteService.ProjectInstaller.resources

     文件        573  2019-01-22 17:42  WriteService\WriteService\Program.cs

     文件        447  2019-01-23 08:52  WriteService\WriteService\ProjectInstaller.cs

     文件       2247  2019-01-23 08:52  WriteService\WriteService\ProjectInstaller.Designer.cs

     文件       6416  2019-01-23 08:52  WriteService\WriteService\ProjectInstaller.resx

     文件       1366  2019-01-22 17:40  WriteService\WriteService\Properties\AssemblyInfo.cs

     文件       3339  2019-01-22 17:43  WriteService\WriteService\WriteService.csproj

     文件       1005  2019-01-22 17:40  WriteService\WriteService.sln

    ..A..H.     40448  2019-01-23 08:52  WriteService\WriteService.v12.suo

............此处省略12个文件信息

评论

共有 条评论