I wanted to know when the computer was turned on, turned off, or stopped. If I turned on my computer, from today's date, then I want to know the time and date it was connected. On shutting down the same thing, if I shut down the computer "normally" from today's date, then capture the date and time. If it was interrupted or forced for example, disconnected out of the socket or lacked power, then capture all this data.
I learned that the reference: System.Diagnostics.Eventing.Reader Contains a set of Classes to get system event logs , but I do not know how to use it.
After much research, I ended up finding the code that generates the start and shutdown event 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);
}
When pulling the schedule and the date that is in the two variables above, the following result appears:
The time and date are practically wrong. I'm in 2018 and the date of the two variables I'm in 0001 . If the date is wrong, then the time is too. And also, the two variables are of the same value.