Click here to Skip to main content
15,667,735 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Or made Windows sleep or woke up Windows (the computer).

Let's say one wants to know when he slept yesterday, or when he woke up today, so he checks in this C# .NET WinForms .exe file that shows him a table of the last such events.

I once started doing this by getting the data of the Event Viewer but I sadly lost the code.

What I have tried:

void refresh()


    addEntriesToDataGridView("Login", 4624);

    addEntriesToDataGridView("Logoff", 4647);

    addEntriesToDataGridView("Lock", 4800);

    addEntriesToDataGridView("Unlock", 4801);

    dataGridViewMain.Sort(dataGridViewMain.Columns[1], ListSortDirection.Descending);

void addEntriesToDataGridView(string name, int id)

    EventLog log = new EventLog("Security");
    var entries = log.Entries.Cast<EventLogEntry>()
                             .Where(x => x.InstanceId == id)
                             .Select(x => new


    foreach (var evl in entries)
        dataGridViewMain.Rows.Add(new object[] { name, evl.TimeGenerated });
Updated 5-Nov-22 5:02am

1 solution

Here is a google search with a number of working solutions: c# read event viewer log - Google Search[^]
Share this answer
John Smith 27 4-Nov-22 8:34am    
Thx, see the updated question please.
Graeme_Grant 4-Nov-22 10:27am    
Have you looked at the event log manually to see if the events are actually being logged? If they are not logged, then that would explain why you are not seeing them.
John Smith 27 4-Nov-22 11:15am    
They are not, and there are many numbers of events and no names like: "Session locked" or "Session unlocked", there's "User account management" which has a lot of it and no fine explanation in its window, I cannot figure out the numbers and the numbers I find in Google don't work for me, Windows 10 - 64bit - 21H2. I'm looking in Security in Event Viewer.
Graeme_Grant 4-Nov-22 17:00pm    
If they are not in the system log, then there is nothing to show.
John Smith 27 5-Nov-22 8:38am    
But why is that? I'm using the ID numbers for these events I found in Google, would you please have a look at them? (please see the simplified question)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900