This documentation is archived and is not being maintained.

Power Management in Windows Forms 

Your Windows Forms applications can take advantage of the power management features in the Windows operating system. Your applications can monitor the power status of a computer and take action when a status change occurs. For example, if your application is running on a portable computer, you might want to disable certain features in your application when the computer's battery charge falls under a certain level.

The .NET Framework provides a PowerModeChanged event that occurs whenever there is a change in power status, such as when a user suspends or resumes the operating system, or when the AC power status or battery status changes. The PowerStatus property of the SystemInformation class can be used to query for the current status, as shown in the following code example.

private void powerModeChanged(System.Object sender, _
Microsoft.Win32.PowerModeChangedEventArgs e)
    int si = SystemInformation.PowerStatus;
    switch (si)
        case BatteryChargeStatus.Low:
            MessageBox.Show("Battery is running low", _ MessageBoxIcon.Exclamation);
        case BatteryChargeStatus.Low:
            MessageBox.Show("Battery is critcally low", _ MessageBoxIcon.Stop);
            // Battery is okay.

Besides the BatteryChargeStatus enumerations, the PowerStatus property also contains enumerations for determining battery capacity (BatteryFullLifeTime) and battery charge percentage (BatteryLifePercent, BatteryLifeRemaining).

You can use the SetSuspendState method of the Application to put a computer into hibernation or suspend mode. If the force argument is set to false, the operating system will broadcast an event to all applications requesting permission to suspend. If the disableWakeEvent argument is set to true, the operating system disables all wake events.

The following code example demonstrates how to put a computer into hibernation.

if (SystemInformation.PowerStatus.BatteryChargeStatus.ToString() == _
        Application.SetSuspenState (PowerState.Hibernate, false, false);

See Also