Friday, July 18, 2008

Battery Status Monitor



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 retrieve power and battery information for notebook, laptop or tablet computers. This class contains five key properties :

§ BatteryChargeStatus. Returns the battery charging status or named charge level.
§ BatteryFullLifetime. Returns the expected duration of operation, in seconds
§ BatteryLifePercent. Returns the percentage of battery life remaining from the current charge.
§ BatteryLifeRemaining. Returns the approximate battery life, in seconds.
§ PowerLineStatus. Returns the current state of the mains power connection.
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.

Application
In this article we will create a Windows Forms application that displays the current power and battery status for the computer.
Controls Used in application:
· Groupbox for placing control in desired location
· Progressbar for displaying charge level.
· Labels for displaying status.
· PictureBox for displaying Power Status , either battery is on charge or is on mains

Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;

namespace BatteryMonitor
{
public partial class Form1 : Form
{
PowerStatus power = SystemInformation.PowerStatus;
public Form1()
{
InitializeComponent();
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + "% available.";
}
else
{
ChargeRemaining.Text = string.Empty;
}

}
private void RefreshStatus()
{
int powerPercent = (int)(power.BatteryLifePercent * 100);
if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; else BatteryIndicator.Value = 0; switch (power.PowerLineStatus) { case PowerLineStatus.Online: pictureBox1.ImageLocation = "images/winamp.png"; groupBox1.Text = "Running On Mains"; float secondsRemaining = power.BatteryLifePercent; if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + "% available.";
}
else
{
ChargeRemaining.Text = string.Empty;
}
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
break;

case PowerLineStatus.Offline:


pictureBox1.ImageLocation = "images/oil.png";
groupBox1.Text = "Running On Battery";
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
break;

case PowerLineStatus.Unknown:

break;
}

}

private void timer1_Tick(object sender, EventArgs e)
{
RefreshStatus();
}

private void Form1_Load(object sender, EventArgs e)
{
RefreshStatus();
timer1.Enabled = true;

}
}
}
You can now execute the program to see the results. If you are using a notebook computer, try removing and re-inserting the power cable to see action.

You can download full app @ http://www.4shared.com/file/56103616/57d62da6/BatteryMonitor_1001.html

3 comments:

vidconn said...

can you post the code to be download?

Amit Ranjan said...

Code has already been pasted here. Copy paste and execute it.

DarkPaladin16 said...

thank you, i was searching for this =)