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

17 comments:

  1. can you post the code to be download?

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

    ReplyDelete
  3. thank you, i was searching for this =)

    ReplyDelete
  4. A lot of important code does not exist in the current context :S

    ReplyDelete
  5. This 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.

    Its totally free. No acts attached.

    ReplyDelete
  6. thanks amit rajan... you help me to complete my battery meter application...

    ReplyDelete
  7. @kadek surya pranata
    Welcome and thanks for appreciating.

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

    }
    this part is which item from the toolbox?

    ReplyDelete
  9. Timer control. Referesh status method is called and timer is set enabled. You can also use Timer class instead of control.

    Amit Ranjan

    ReplyDelete
  10. Do you know how to implement battery monitor for both primary and secondary using WMI?

    ReplyDelete
  11. Do you have this file anymore the 4shared link doesn't exist anymore =(

    ReplyDelete
  12. sir 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
    looking for ur reply

    ReplyDelete
  13. Your code is redundant. for example your can reuse the variable of

    secondsRemaining = 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.

    ReplyDelete
  14. Hi,

    Have you any idea to calculate Battery Charge/Discharge Cycle Count?

    Sanjay

    ReplyDelete
  15. The quality of
    content is fine and the conclusion is good.
    Thanks for the post....
    BRANDED LAPTOPS & DESKTOPS

    ReplyDelete