登录
  • 欢迎访问 Sharezer Blog

【小工具】查询开关机时间

C# sharezer 4491次浏览 已收录 0个评论

原理

调用Window的事件(EventLog),通过开机与关机的事件ID,得到开关机记录。

界面

【小工具】查询开关机时间

源码

const long ON_ID = 2147489653; //开机
const long OFF_ID = 2147489654; //关机

private void BtnSearch_Click(object sender, EventArgs e)
{
    mainListBox.Items.Clear();
    EventLog eventLog = new EventLog();
    eventLog.Log = "System";//"Application"应用程序, "Security"安全, "System"系统
    foreach (EventLogEntry l in eventLog.Entries)
    {
        if (l.InstanceId == OFF_ID) // shutdown
        {
            AddMessage("关机时间:" + l.TimeGenerated);
        }

        if (l.InstanceId == ON_ID) // shutup
        {
            AddMessage("开机时间:" + l.TimeGenerated);
        }
    }
}

private void AddMessage(string message)
{
    mainListBox.Items.Add(message);
    mainListBox.SelectedIndex = mainListBox.Items.Count - 1;
    mainListBox.SelectedIndex = -1;
}


Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明【小工具】查询开关机时间
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址