Determining current laptop battery drain in .NET

My laptop battery’s lifetime has dropped significantly of late, and I wanted some means of determining the actual current being used by the system – sort of like a Power Meter for the laptop battery. I‘ve actually been looking for such a tool off and on for months. I’ve seen things you have to pay for, things that are quite old, and things you can do yourself in C++…

Well I don’t want to pay for something that tells me the power usage in mw (milliwatts). Nor do I want something with “Vista” style. Nor do I want to deal with c++.

1. Performance Monitor

Not sure why I didn’t think to look here – maybe because it’s a big mess – but it turns out the power utilization in mW is in a couple places in windows Performance Monitor:

Under Power Meter –> Power:

image

Do not use the _Total option – at least on my machine it is incorrect. Use all instances, then look to see which instance corresponds to your actual battery. (note: this only works while your laptop is running on battery power. “Power Meter” returns 0 when you are on AC power)

 

You can also use Battery Status –> Discharge Rate. There is also a Charge Rate that will have a value when the battery is charging. The only catch is that access to Battery Status requires Administrator privs.

 

2. .NET Performance Counter – Forms control

Forget WMI queries and System.Management – Be lazy! Visual Studio has a Server Explorer tab that allows you to easily add performance counters to your Windows Forms project.

Open Server Explorer –> You computer –> Performance Counters

image

Find Power Meter, expand Power, and drag whichever power meters you need onto your form. (In my case I just needed Power Meter (0).

image

This will add performanceCounter1 to your project. To get the current power usage in mW, just access performanceCounter1.NextValue().

 

3. .NET Performance Counter – Code

Even easier, once you know the Category (“Power Meter”), Counter (“Power”) and Instance (“Power Meter (0)”), you can create the performance counter in code like so:

(Add a textbox and a button to your form), use this as the button event handler

        private void button1_Click(object sender, EventArgs e)
        {
            var powerCounter = new System.Diagnostics.PerformanceCounter("Power Meter", "Power", "Power Meter (0)", true);
            textBox1.Text = powerCounter.NextValue().ToString();
        }

 

Chances are I will release a (free, open source) tool based on this, but it’s painfully simple to just get that current value. (Now to start seeing the effect of the backlight, and hard drive, and cpu load…)