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
can you post the code to be download?
ReplyDeleteCode has already been pasted here. Copy paste and execute it.
ReplyDeletethank you, i was searching for this =)
ReplyDeleteA lot of important code does not exist in the current context :S
ReplyDeleteThis is just a way to provide help on its baseic areas. If u need full source download the file from softpedia and decompile it using reflector.
ReplyDeleteIts totally free. No acts attached.
thanks amit rajan... you help me to complete my battery meter application...
ReplyDelete@kadek surya pranata
ReplyDeleteWelcome and thanks for appreciating.
Find the right batteries supplier is not hard if you check out Laptopbatteresinc.com.au. You can get the procell batteries which available in several sizes and voltage.All of the batteries come with cheaper price.Please contact us to know details.Dell Inspiron 9300 battery
ReplyDeleteDELL Latitude D830 Battery
DELL Precision M65 Battery
Dell Inspiron 700m battery
DELL Inspiron 1300 battery
DELL Inspiron B120 battery
DELL Inspiron 1200 battery
Dell Latitude X200 Battery
Dell Latitude X300 battery
Dell Latitude C400 battery
Dell Latitude D400 battery
Dell Latitude D410 battery
Dell Latitude D420 battery
Dell Latitude D800 battery
Dell Inspiron 2000 Battery
Dell Inspiron 2100 Battery
Dell Latitude L400 Battery
private void Form1_Load(object sender, EventArgs e)
ReplyDelete{
RefreshStatus();
timer1.Enabled = true;
}
this part is which item from the toolbox?
Timer control. Referesh status method is called and timer is set enabled. You can also use Timer class instead of control.
ReplyDeleteAmit Ranjan
Do you know how to implement battery monitor for both primary and secondary using WMI?
ReplyDeleteIs a great blog, keep it.
ReplyDeleteDo you have this file anymore the 4shared link doesn't exist anymore =(
ReplyDeletesir i need to get the battery status and then if battery was less than 5o percent i need to give some message as a alarm one can u help me
ReplyDeletelooking for ur reply
Your code is redundant. for example your can reuse the variable of
ReplyDeletesecondsRemaining = power.BatteryLifePercent;
inside the timer1.
It's NOT a good practice to put some codes in the Form1() constructor. A good practice is to just initialize global variable inside constructor. Use Form1_Load Instead.
Anyway good tutorial.
Hi,
ReplyDeleteHave you any idea to calculate Battery Charge/Discharge Cycle Count?
Sanjay
The quality of
ReplyDeletecontent is fine and the conclusion is good.
Thanks for the post....
BRANDED LAPTOPS & DESKTOPS