How to

enumerate batteries using WMI

Published: 12. February 2012 | Updated: 12. February 2012
License: Microsoft Public License (MS-PL)
Categories: Windows » Hardware
Tags: C# Hardware Windows WMI
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System;
using System.Collections.Generic;
using System.Management; // requires adding System.Management reference to project

Code

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery");
ManagementObjectCollection collection = searcher.Get();

var items = new List<Win32_Battery>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_Battery();
    item.Availability = (ushort?)obj["Availability"];
    item.BatteryRechargeTime = (uint?)obj["BatteryRechargeTime"];
    item.BatteryStatus = (ushort?)obj["BatteryStatus"];
    item.Caption = (string)obj["Caption"];
    item.Chemistry = (ushort?)obj["Chemistry"];
    item.ConfigManagerErrorCode = (uint?)obj["ConfigManagerErrorCode"];
    item.ConfigManagerUserConfig = (bool?)obj["ConfigManagerUserConfig"];
    item.CreationClassName = (string)obj["CreationClassName"];
    item.Description = (string)obj["Description"];
    item.DesignCapacity = (uint?)obj["DesignCapacity"];
    item.DesignVoltage = (ulong?)obj["DesignVoltage"];
    item.DeviceID = (string)obj["DeviceID"];
    item.ErrorCleared = (bool?)obj["ErrorCleared"];
    item.ErrorDescription = (string)obj["ErrorDescription"];
    item.EstimatedChargeRemaining = (ushort?)obj["EstimatedChargeRemaining"];
    item.EstimatedRunTime = (uint?)obj["EstimatedRunTime"];
    item.ExpectedBatteryLife = (uint?)obj["ExpectedBatteryLife"];
    item.ExpectedLife = (uint?)obj["ExpectedLife"];
    item.FullChargeCapacity = (uint?)obj["FullChargeCapacity"];
    item.InstallDate = (DateTime?)obj["InstallDate"];
    item.LastErrorCode = (uint?)obj["LastErrorCode"];
    item.MaxRechargeTime = (uint?)obj["MaxRechargeTime"];
    item.Name = (string)obj["Name"];
    item.PNPDeviceID = (string)obj["PNPDeviceID"];
    item.PowerManagementCapabilities = (ushort[])obj["PowerManagementCapabilities"];
    item.PowerManagementSupported = (bool?)obj["PowerManagementSupported"];
    item.SmartBatteryVersion = (string)obj["SmartBatteryVersion"];
    item.Status = (string)obj["Status"];
    item.StatusInfo = (ushort?)obj["StatusInfo"];
    item.SystemCreationClassName = (string)obj["SystemCreationClassName"];
    item.SystemName = (string)obj["SystemName"];
    item.TimeOnBattery = (uint?)obj["TimeOnBattery"];
    item.TimeToFullCharge = (uint?)obj["TimeToFullCharge"];

    items.Add(item);
}

Class

public class Win32_Battery
{
    public ushort? Availability;
    public uint? BatteryRechargeTime;
    public ushort? BatteryStatus;
    public string Caption;
    public ushort? Chemistry;
    public uint? ConfigManagerErrorCode;
    public bool? ConfigManagerUserConfig;
    public string CreationClassName;
    public string Description;
    public uint? DesignCapacity;
    public ulong? DesignVoltage;
    public string DeviceID;
    public bool? ErrorCleared;
    public string ErrorDescription;
    public ushort? EstimatedChargeRemaining;
    public uint? EstimatedRunTime;
    public uint? ExpectedBatteryLife;
    public uint? ExpectedLife;
    public uint? FullChargeCapacity;
    public DateTime? InstallDate;
    public uint? LastErrorCode;
    public uint? MaxRechargeTime;
    public string Name;
    public string PNPDeviceID;
    public ushort[] PowerManagementCapabilities;
    public bool? PowerManagementSupported;
    public string SmartBatteryVersion;
    public string Status;
    public ushort? StatusInfo;
    public string SystemCreationClassName;
    public string SystemName;
    public uint? TimeOnBattery;
    public uint? TimeToFullCharge;
}
Send us feedback about this snippet »



Related Snippets: