简体   繁体   中英

How to get Battery Health Using C#

I'm trying to get two values, DesignCapacity and FullChargeCacity , but they are not returning a value.

I'm using the code below:

public static bool CheckBattery()
{
    bool _testResult = false;
    string computer = ".";
    try
    {
        ManagementScope scp = new ManagementScope(@"\\" + computer + @"\root\cimv2");
        scp.Connect();

        SelectQuery sql = new SelectQuery("CIM_Battery");
        ManagementObjectSearcher mos = new ManagementObjectSearcher(scp,sql);
        ManagementObjectCollection moc = mos.Get();
        List<string> resultList = new List<string>();
        foreach (ManagementObject o in moc)
        {
            foreach (PropertyData property in o.Properties)
            {
                resultList.Add( property.Name + ":" +property.Value);
            }

            File.WriteAllLines("bat.txt", resultList.ToArray());

            //UInt16 availability = (UInt16)o["Availability"];
            //resultList.Add("Availability," + availability.ToString());
            //UInt16 batterystatus = (UInt16)o["BatteryStatus"];
            //resultList.Add("BatteryStatus," + batterystatus.ToString());
            //string status = (string)o["Status"];
            //resultList.Add("Status," + status);
            //UInt32 Desigcap = (UInt32)o["DesignCapacity"];
        }
    }
    catch (Exception ex)
    {
        string errr = ex.Message;
    }
    return _testResult;
}

How Can I avoid to get null values in this properties? In fact iterating the objects im getting this information:

Availability:2
BatteryRechargeTime:
BatteryStatus:2
Caption:Internal Battery
Chemistry:2
ConfigManagerErrorCode:?
ConfigManagerUserConfig:?
CreationClassName:Win32_Battery
Description:Internal Battery
DesignCapacity:?
DesignVoltage:17119
DeviceID:3338SANYO AL12A32
ErrorCleared:?
ErrorDescription:?
EstimatedChargeRemaining:100
EstimatedRunTime:71582788
ExpectedBatteryLife:
ExpectedLife:?
FullChargeCapacity:?
InstallDate:?
LastErrorCode:
MaxRechargeTime:
Name:AL12A32
PNPDeviceID:
PowerManagementCapabilities:System.UInt16[]
PowerManagementSupported:False
SmartBatteryVersion:
Status:OK
StatusInfo:?
SystemCreationClassName:Win32_ComputerSystem
SystemName:MX02L180
TimeOnBattery:?
TimeToFullCharge:?

To get those particular values you need to do separate queries against different classes.

DesignCapacity can be queried from BatteryStaticData

FullChargeCacity can be queried from BatteryFullChargedCapacity

You'll need to use a different scope in the code also for the query. These classes are found in root/WMI instead of root/cimv2

string scope = "root/WMI";
string query = "SELECT DesignedCapacity FROM BatteryStaticData";

using (ManagementObjectSearcher batteriesQuery = new ManagementObjectSearcher(scope, query))
{
    using (ManagementObjectCollection batteries = batteriesQuery.Get())
    {
        foreach (ManagementObject battery in batteries)
        {
            if (battery != null)
            {
                foreach (var property in battery.Properties)
                {
                    Console.Log("Property name: " + property.Name + " Property value: " + property.Value);
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM