原理
调用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; }