zoukankan      html  css  js  c++  java
  • c# windows service

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    
    namespace WindowsServiceTest
    {
        public partial class ServiceTest : ServiceBase
        {
            private DataSet _Ds = new DataSet();
            //设置xml文件保存路径
            private string _FilePath = @"D:记录开关机时间.xml";
    
            public ServiceTest()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                //每隔一分钟记录一次更新一次关机时间
                System.Timers.Timer timer = new System.Timers.Timer(60000);
                timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Click);
                timer.AutoReset = true;
                timer.Enabled = true;
    
                //判断文件是否存在
                if (!System.IO.File.Exists(_FilePath))
                {
                    CreateDataTable();
                }
                else
                {
                    _Ds.ReadXml(_FilePath, XmlReadMode.ReadSchema);
                }
    
                this.Add("开机时间", DateTime.Now);
                this.Add("关机时间", DateTime.Now);
                this.SaveToXml();
            }
    
            protected override void OnStop()
            {
                this.Update("关机时间", DateTime.Now);
                this.SaveToXml();
            }
    
            private void Timer_Click(Object sender,System.Timers.ElapsedEventArgs e)
            {
                this.Update("关机时间",DateTime.Now);
                this.SaveToXml();
            }
    
            private void CreateDataTable()
            {
                System.Data.DataTable Dt = new DataTable("OPENCLOSE");
                Dt.Columns.Add("TimeType", typeof(string));
                Dt.Columns.Add("OperTime", typeof(DateTime));
                _Ds.Tables.Add(Dt);
            }
    
    
            //添加开关机时间记录
            private bool Add(string TimeType, DateTime OperTime)
            {
                if (_Ds.Tables.Count == 0)
                    return false;
                DataTable Dt = _Ds.Tables["OPENCLOSE"];
                if (Dt == null)
                    return false;
                DataRow dr = Dt.NewRow();
                dr["TimeType"] = TimeType;
                dr["OperTime"] = OperTime;
    
                Dt.Rows.Add(dr);
                return true;
    
            }
    
    
            //更新关机时间
            private bool Update(string OperTime, DateTime UpdateTime)
            {
                if (_Ds.Tables.Count == 0)
                    return false;
                DataTable Dt = _Ds.Tables["OPENCLOSE"];
                if (Dt == null)
                    return false;
                DataRow Dr = Dt.Rows[Dt.Rows.Count - 1];
    
                Dr["TimeType"] = OperTime;
                Dr["OperTime"] = UpdateTime;
                return true;
            }
    
    
            //保存到xml文件
            private void SaveToXml()
            {
                if (_Ds == null)
                    return;
                _Ds.WriteXml(_FilePath, XmlWriteMode.WriteSchema);
            }
        }
    }
    

      服务安装脚本

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe WindowsServiceTest.exe
    Net Start ServiceTest
    sc config ServiceTest start= auto
    pause
    

      服务卸载脚本

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u WindowsServiceTest.exe
    pause
    

      

  • 相关阅读:
    Luogu 4206 [NOI2005]聪聪与可可
    【Luogu】P3708Koishi的数字游戏(数论)
    【Luogu】P1850换教室(期望DP)
    【Luogu】P1231教辅的组成(拆点+Dinic+当前弧优化)
    【Luogu】P3865ST表模板(ST表)
    【Luogu】P3376网络最大流模板(Dinic)
    【Luogu】P1005矩阵取数游戏(高精度+DP)
    【Luogu】P2324骑士精神(IDA*)
    【Luogu】P3052摩天大楼里的奶牛(遗传算法乱搞)
    洛森地图半成品
  • 原文地址:https://www.cnblogs.com/yufenghou/p/3300489.html
Copyright © 2011-2022 走看看