3
I wanted to know when the computer was switched on, off or interrupted. If I turned on my computer from today’s date, then I want to know the time and date it was turned on. On shutdown the same thing, if I turned off the computer "normally" from today’s date, then capture the date and time. If you were interrupted or forced for example, disconnected outside the socket or lacked power, then capture all these data.
I heard the reference: System.Diagnostics.Eventing.Reader Contains a set of Classes to get the system event logs, but do not know how to use.
After a lot of research, I ended up finding the code that generates the event of beginning and shutdown of the system below.
if (EventLog.Exists("System"))
{
var log = new EventLog("System", Environment.MachineName, "EventLog");
var entries = new EventLogEntry[log.Entries.Count];
log.Entries.CopyTo(entries, 0);
var startupTimes = entries.Where(x => x.InstanceId == 2147489653).Select(x => x.TimeGenerated);
var shutdownTimes = entries.Where(x => x.InstanceId == 2147489654).Select(x => x.TimeGenerated);
}
Time to pull the timetable and the date which is in the two variables above, the following result appears:
The time and date are practically wrong. I’m in 2018 and on the date of the two variables I am in 0001. If the date is wrong then it means the time is also. Also, the two variables have the same value.