Introduction
This article explains how to get the details of the battery connected to your
system. Here I will get the information from the Win32_Battery class.
What Win32_Battery is
The Win32_Battery WMI class a represents battery connected to the computer
system..
Design
Create a new Windows Forms Application Project.
Add one button control to the form.
Design your screen as in the following screen:
Next add a reference for "System.Management".
To add the reference use the following procedure.
Go to Solution Explorer, select the project and right-click on that and choose
"Add Reference" from the menu.
A window will open; in that choose the ".Net" tab.
It will show a list. In that list, choose "System.Management" and click the "OK"
Button.
Now go to code view
Add the namespace "using System.Management;".
Write the following code in the Button Click event:
private
void button1_Click(object
sender, EventArgs e)
{
SelectQuery Sq =
new SelectQuery("Win32_Battery");
ManagementObjectSearcher
objOSDetails = new
ManagementObjectSearcher(Sq);
ManagementObjectCollection
osDetailsCollection = objOSDetails.Get();
StringBuilder sb =
new StringBuilder();
foreach (ManagementObject
mo in osDetailsCollection)
{
sb.AppendLine(string.Format("Availability:
{0}", (ushort)mo["Availability"]));
sb.AppendLine(string.Format("BatteryRechargeTime:
{0}", (string)mo["BatteryRechargeTime"]));
sb.AppendLine(string.Format("BatteryStatus:
{0}", (ushort)mo["BatteryStatus"]));
sb.AppendLine(string.Format("Caption:
{0}", (string)mo["Caption"]));
sb.AppendLine(string.Format("Chemistry:
{0}", (ushort)mo["Chemistry"]));
sb.AppendLine(string.Format("ConfigManagerErrorCode:
{0}", (string)mo["ConfigManagerErrorCode"]));
sb.AppendLine(string.Format("InstallDate:
{0}", Convert.ToDateTime(mo["InstallDate"]).ToString()));
sb.AppendLine(string.Format("ConfigManagerUserConfig:
{0}", (string)mo["ConfigManagerUserConfig"]));
sb.AppendLine(string.Format("CreationClassName
: {0}", (string)mo["CreationClassName"]));
sb.AppendLine(string.Format("Description:
{0}", (string)mo["Description"]));
sb.AppendLine(string.Format("DesignCapacity
: {0}", (string)mo["DesignCapacity"]));
sb.AppendLine(string.Format("DesignVoltage:
{0}", (ulong)mo["DesignVoltage"]));
sb.AppendLine(string.Format("DeviceID
: {0}", (string)mo["DeviceID"]));
sb.AppendLine(string.Format("ErrorCleared:
{0}", (string)mo["ErrorCleared"]));
sb.AppendLine(string.Format("ErrorDescription
: {0}", (string)mo["ErrorDescription"]));
sb.AppendLine(string.Format("EstimatedChargeRemaining:
{0}", (ushort)mo["EstimatedChargeRemaining"]));
sb.AppendLine(string.Format("EstimatedRunTime
: {0}", mo["EstimatedRunTime"]).ToString());
sb.AppendLine(string.Format("ExpectedBatteryLife:
{0}", (string)mo["ExpectedBatteryLife"]));
sb.AppendLine(string.Format("ExpectedLife
: {0}", (string)mo["ExpectedLife"]));
sb.AppendLine(string.Format("FullChargeCapacity:
{0}", (string)mo["FullChargeCapacity"]));
sb.AppendLine(string.Format("LastErrorCode
: {0}", (string)mo["LastErrorCode"]));
sb.AppendLine(string.Format("MaxRechargeTime:
{0}", (string)mo["MaxRechargeTime"]));
sb.AppendLine(string.Format("Name
: {0}", (string)mo["Name"]));
sb.AppendLine(string.Format("PNPDeviceID:
{0}", (string)mo["PNPDeviceID"]));
sb.AppendLine(string.Format("PowerManagementSupported
: {0}", mo["PowerManagementSupported"]).ToString());
sb.AppendLine(string.Format("SmartBatteryVersion:
{0}", (string)mo["SmartBatteryVersion"]));
sb.AppendLine(string.Format("Status
: {0}", (string)mo["Status"]));
sb.AppendLine(string.Format("SystemCreationClassName
: {0}", (string)mo["SystemCreationClassName"]));
sb.AppendLine(string.Format("SystemName:
{0}", (string)mo["SystemName"]));
sb.AppendLine(string.Format("TimeToFullCharge
: {0}", (string)mo["TimeToFullCharge"]));
sb.AppendLine(string.Format("TimeOnBattery:
{0}", (string)mo["TimeOnBattery"]));
UInt16[] PowerManagement = (UInt16[])mo["PowerManagementCapabilities"];
foreach (uint
version in PowerManagement)
{
sb.AppendLine(string.Format("PowerManagementCapabilities:
{0}", version.ToString()));
}
}
MessageBox.Show(sb.ToString()); }
In the code above I am getting the information from
Win32_Battery and showing it in a Message Box on a button click.
SelectQuery
It represents a WQL (WMI Query Language) SELECT data query.
ManagementObjectSearcher
It retrieves a collection of management objects based on a specified query.
This class is one of the more commonly used entry points to retrieving
management information.
ManagementObjectCollection
It represents various collections of management objects retrieved using WMI.
Now build your application. Click on the button; it will show the Battery details
in a Message box.
Thank you.