Find out battery charge capacity in percentage using C# or .NET
Don't have a laptop to test with, but I'm guessing you could use the WMI class Win32_Battery.
It has two fields that look interesting - DesignCapacity
, which tells you
Design capacity of the battery in milliwatt-hours.
and FullChargeCapacity
, which has the fascinating note that
Full charge capacity of the battery in milliwatt-hours. Comparison of the value to the DesignCapacity property determines when the battery requires replacement.
So my guess is that you can use WMI to read these two values, and then calculate FullChargeCapacity/DesignCapacity
to find the battery health percentage number.
EDIT
Here's a brief example of accessing WMI information using C#. I first added a reference to the System.Management
assembly. Then:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Management.ObjectQuery query = new ObjectQuery("Select * FROM Win32_Battery");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject mo in collection)
{
foreach (PropertyData property in mo.Properties)
{
Console.WriteLine("Property {0}: Value is {1}", property.Name, property.Value);
}
}
}
}
}
Also, note that you are basically running a SQL-like query against WMI, so you can vary that if you want. Windows Management Instrumentation Query Language
, or WQL
, is what you want to search for to learn more about it.
Also take a look at ahawker's answer, it may end up being more helpful if WMI isn't properly capturing the battery data, as he notes.
It seems that you are looking for the values of FullChargeCapacity
, DesignCapacity
and CurrentCapacity
. As someone who has solved this problem before, let me make a few comments.
The first route normally taken would be through a WMI query (Win32_Battery
). However, on the test laptops I ran the WMI query (Win32_Battery
) against, which included multiple manufacturers, I consistently ran into the problem of FullChargeCapacity
always returning zero. Since that didn't work, I re-wrote my solution using Win32 API and was successfully able to get accurate values that way.
Hopefully, WMI will work for you. However, if you experience the same issues I did, here is a summary of the steps required for Win32API.
Use
SetupDiGetClassDevs
to get a device handle to the battery (GUID_DEVCLASS_BATTERY
).Use
SetupDiEnumDeviceInterfaces
to get the device data (SP_DEVICE_INTERFACE_DATA
).Use
SetupDiGetDeviceInterfaceDetail
to get the device path (SP_DEVICE_INTERFACE_DETAIL_DATA
).Use
CreateFile
with the device path to get handle to battery.Use
DeviceIoControl
with battery handle,IOCTL_BATTERY_QUERY_TAG
to retrieve battery query info (BATTERY_QUERY_INFORMATION
).Use
DeviceIoControl
with battery handle,IOCTL_BATTERY_QUERY_INFORMATION
and marshalledstructs
to to retrieve battery info (BATTERY_INFORMATION
).
Also see the Enumerating Battery Devices post on MSDN as I found that quite helpful.
I can post my solution if necessary but with all the native struct
definitions, it ends up around 500 lines of code.
Example source code: https://gist.github.com/ahawker/9715872
No need to unnecessary complicate things. Try something like:
using System.Management;
PowerStatus pwr = SystemInformation.PowerStatus;
String strBatteryChargingStatus;
strBatteryChargingStatus = pwr.BatteryChargeStatus.ToString();
MessageBox.Show("battery charge status : " + batterystatus);
String strBatterylife;
strBatterylife = pwr.BatteryLifePercent.ToString();
MessageBox.Show("Battery life: "+batterylife);
In this way you can get all of the battery information.