Do anyone know if its possible to detect if the last boot up was preceded by a power-failure in Windows ?
My reason for wanting to detect this is to know when to re-scan files I frequently modify on disk with my service.
If there is a normal startup, I can be quite sure that the shutdown went smooth and the data that was in in-memory file buffers was flushed to disk.
View Shutdown and Restart Log from Event ViewerOpen Event Viewer (press Win + R[Run] and type eventvwr). In the left pane, open “Windows Logs >> System.” In the middle pane, you will get a list of events that occurred while Windows was running. You can sort the event log with the Event ID.
Type the following command to view the event logs and press Enter: wevtutil qe System /q:"*[System[(EventID=41) or (EventID=1074) or (EventID=6006) or (EventID=6005) or (EventID=6008)]]" /c:100 /f:text /rd:true.
Detecting powerfailure will only cover one possible reason for abnormal termination. The safe way of doing this is to create a marker file when you start, then remove it when you cleanly shutdown. If the marker file exists on next startup you know that your service was not cleanly terminated.
If you want to look for powerloss only, reading the event log will tell you whether the last shutdown was unexpected.
Why not to check if your process terminated gracefully instead? Place some special marker in registry or in file system, that will mean, that your process is still working and delete it on gracefull shutdown. Then check it at every startup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With