Not sure why that isn't working, but another way to do it:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C powercfg /batteryreport";
process.StartInfo = startInfo;
process.Start();
This will output the battery life to a file: C:/Windows/System32/battery-report.html
You can then scrape that page. It's a bit overkill, but there might be more data in there that you can use for your diagnostics.
Edit: also a powershell
class in .net (System.Management.Automation) assembly. So you could make a .ps1 with the line:
(Get-WmiObject win32_battery).estimatedChargeRemaining
PowerShell psinstance = PowerShell.Create();
psinstance.AddScript(scriptPath);
var results = psinstance.Invoke();
Edit2: The CIM (common information model) classes are parent classes upon which the WMI classes are built. win32_xxx
classes monitor and manage system hardware and features and are located in in CIM. Instead of pulling from CIM_Battery
parent class, pull from the win32_battery
class instead.
So your computer has a battery? – Ultrasonic54321 – 2018-03-23T22:27:57.563
@Ultrasonic54321 Yes, it does. – ifconfig – 2018-03-24T03:25:08.160