C#(windowsservice)-(記錄開關機)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Text;

using System.Timers;

 

namespace 记录开关机时间

{

    public partial class Service1 : ServiceBase

    {

        private DataSet _Ds =new DataSet();

 

        //设置XML文件保存路径

        private string_FilePath = @"D:\记录开关机时间.xml";

 

        public Service1()

        {

           InitializeComponent();

        }

 

        ///<summary>

        ///启动服务

        ///</summary>

        ///<param name="args"></param>

        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();

        }

 

        ///<summary>

        ///停止服务

        ///</summary>

        protected override void OnStop()

        {

            this.Update("关机时间", DateTime.Now);

            this.SaveToXml();

        }

 

        ///<summary>

        ///间隔更新关机时间

        ///</summary>

        ///<param name="sender"></param>

        ///<param name="e"></param>

        private voidTimer_Click(Object sender, ElapsedEventArgs e)

        {

            this.Update("关机时间", DateTime.Now);

            this.SaveToXml();

        }

 

        ///<summary>

        ///创建数据表

        ///</summary>

        private voidCreateDataTable()

        {

            //_Ds = new DataSet();

           System.Data.DataTable Dt = new DataTable("OPENCLOSE");

           Dt.Columns.Add("TimeType", typeof(string));//开机还是关机还是更新

           Dt.Columns.Add("OperTime", typeof(DateTime));

           _Ds.Tables.Add(Dt);

        }

 

        ///<summary>

        ///添加开关机时间记录

        ///</summary>

        ///<param name="TimeType"></param>

        ///<paramname="OperTime"></param>

        ///<returns></returns>

        private bool Add(string TimeType, DateTimeOperTime)

        {

            if (_Ds.Tables.Count == 0) returnfalse;

 

            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;

        }

 

        ///<summary>

        ///更新关机时间

        ///</summary>

        ///<param name="OperTime"></param>

        ///<param name="UpdateTime"></param>

        ///<returns></returns>

        private bool Update(string OperTime, DateTimeUpdateTime)

        {

            if (_Ds.Tables.Count == 0) returnfalse;

 

            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;

        }

 

        ///<summary>

        ///保存到XML文件

        ///</summary>

        private voidSaveToXml()

        {

            if (_Ds == null) return;

           _Ds.WriteXml(_FilePath, XmlWriteMode.WriteSchema);

        }

 

    }

}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值