I am trying to get the battery percentage of my laptop, with the following code:
using System.Windows.Threading;
using System.Management.Instrumentation;
using System.Diagnostics;
using System.Management;
private void bateria()
{
ManagementClass wmi = new ManagementClass("win32_battery");
var baterias = wmi.GetInstances();
foreach (var bateriass in baterias)
{
por = Convert.ToInt32(bateriass["por"]);
label2.Content = por;
}
}
But I get this error:
I already have the "system.management" reference added. I would greatly appreciate your help.
Have you tried to use a class from Windows Forms itself called: PowerStatus ?
In order to use this class, you would have to do it this way (in your case):
In order to use this class you will have to do a:
The PowerStatus class has 5 parameters that you can "play" with:
PowerLineStatus : Current system power status.
BatteryChargeStatus : Current battery charge status.
BatteryFullLifetime : Duration of the main battery in seconds
BatteryLifePercent : Approximate amount of battery charge remaining.
BatteryLifeRemaining : Approximate number of seconds of battery time remaining.
PS: I recommend that you use the Binding to be able to make links without referring to the "objects" by names.
I hope it helps.