Tuesday, June 15, 2010

Readout Battery Charge Level, Battery Service Life etc.

From .Net framework version 2.0 upwards a new, very interesting class is available.
What was earlier only possible with API - calls is now pretty easy with .Net means.
I speak of the class PowerStatus, which is able to get information about the battery contained in the notebook.
The following example demonstrates the possibilites of this class, for example how to read the battery service life or the charge level:
            /* BatteryChargeStatus describes the current battery charge level, the result is an enumeration containing the following values:
            - High
            - Low
            - Critical
            - Charging
            - No SystemBattery
            - Unknown */

            string ChargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus.ToString();

            // BatteryFullLifetime saves the maximal lifespan of the battery, meaning how long the battery can hold according to the manufacturer
            float FullLifeTime = SystemInformation.PowerStatus.BatteryFullLifetime;
      
            // reads the remaining capacity of the battery in percent
            float RemainingPercent = SystemInformation.PowerStatus.BatteryLifePercent;

            // reads the reamaining capacity of the battery in seconds
            float RemainingSeconds = SystemInformation.PowerStatus.BatteryLifeRemaining;

            // Determines, whether the notebook is connected to the power (Online) or running via battery (offline).
            string OnBattery = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus.ToString();

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete