Is it possible in .NET to ascertain whether my application is closing due to Windows being given a shutdown command (as opposed to any old application closing) in order to either write out some temporary cache files or even block the shutdown long enough to prompt for user input?
Whilst my current scope involves a Winform app and a windows service, I am interested in understanding this in a generic way if possible
SystemEvents.SessionEnding looks like a good starting point for you. That article talks about the event sequence involved when a logout/shutdown is occurring.
In general, you will want to handle the WM_QUERYENDSESSION
Windows message. This will give your application a chance to do cleanup, or to block the shutdown if it's really necessary.
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