I want to check if the system has a battery or not and then get the percentage of battery remaining. I'm not sure how to check if a battery exists, but Here is the code I tried to get the percentage of battery remaining which doesn't work since The code only returns 0:
public double GetBatteryPercent()
{
ManagementClass wmi = new ManagementClass("Win32_Battery");
ManagementObjectCollection allBatteries = wmi.GetInstances();
double batteryLevel = 0;
foreach (var battery in allBatteries)
{
batteryLevel = Convert.ToDouble(battery["EstimatedChargeRemaining"]);
}
return batteryLevel;
}
Also I'm using visual studio 2015 on windows 7 could that be the potential problem that's causing this?