I use the 'Suspend' status of the e.mode argument to trigger actions when the system goes into shutdown.
//Register for event
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
//Do stuff when system shutsdown
private static void SystemEvents_PowerModeChanged(object sender,
PowerModeChangedEventArgs e)
{
if (e.Mode.ToString() == "Suspend")
{
//do stuff
}
You can also use the following event to trigger in the event of Hybernation:
//Register for session ending events
SystemEvents.SessionEnding += c_SessionEndedEvent;
//The delegate handler
private static void c_SessionEndedEvent(object sender, SessionEndingEventArgs e)
{
//Do stuff here
}
You can also use the PowerModeChanged event to specify behaviour when the system wakes back up.
This will not work when the system goes to sleep though... however I have never come across a situation where I have wanted to do this.