BlackWaspTM
System Information
.NET 2.0+

Creating a Battery Power Status Monitor

by Richard Carr, published at http://www.blackwasp.co.uk/PowerStatus.aspx

Sometimes programs need to know the power status of a computer. An example is Windows Update, which often requires a computer to be running on mains power before updates are installed. This article explains how to read the power status and battery life.

PowerStatus Class

The .NET framework provides a class named "PowerStatus" that can be used to hold power and battery information for notebook, laptop or tablet computers. This class contains five key properties that can be used when creating a battery power status monitor application:

  • BatteryChargeStatus. Returns the battery charging status or named charge level. The charge level is one of High, Low or Critical.
  • BatteryFullLifetime. Returns the expected duration of operation, in seconds, that can be achieved from a full battery charge.
  • BatteryLifePercent. Returns the percentage of battery life remaining from the current charge.
  • BatteryLifeRemaining. Returns the approximate battery life, in seconds, remaining from the current charge.
  • PowerLineStatus. Returns the current state of the mains power connection.

In order to query the power and battery status for a computer, a PowerStatus object is required. This can be obtained by querying the static PowerStatus property of the SystemInformation class.

Creating the Application

In this article we will create a Windows Forms application that displays the current power and battery status for the computer. This utility will continuously update the status retrieved from four of the five properties described above.

To begin, create a new Windows Forms application named "BatteryMonitor". The new application will include a blank form with a default name. Change the form name to "BatteryMonitorForm" before continuing.

The Main Form

The battery power monitor program will include a single form to display the current battery and power connection status. The form will include a checkbox to indicate if the mains power is connected, a status bar indicating the percentage of charge remaining and two labels to show the charge time remaining and the current battery status. The form will also include a timer that will be used to automatically refresh the information periodically.

Add the following controls to the form. You may wish to organise these controls into groups in the form or add descriptive labels or a form title. On completing this step, the form should be similar to that shown beneath the table.

Control NameTypeSpecial PropertiesPurpose
MainsPowerCheckBoxAutoCheck = False
Text = "Running on Mains Power"
ThreeState = True
Indicates if the computer is connected to mains power. AutoCheck is disabled so that user clicks are ignored. ThreeState is required as the power status can be Online, Offline or Unknown. Unknown will be represented by a shaded box.
BatteryIndicatorProgressBarMaximum = 100
Minimum = 0
Style = Continuous
Shows a graphical representation of the percentage of battery charge remaining.
BatteryLifeRemainingLabelShows the estimated remaining battery life in minutes.
BatteryStatusLabelShows the current status of the battery.
RefreshTimerTimerInterval = 1000Causes the refresh of the form data periodically. The Interval of 1000ms indicates a refresh rate of once per second.

Battery Power Status Monitor Form

28 April 2008