3

So my method is as follows:

PowerStatus powerStatus = SystemInformation.PowerStatus;

if (powerStatus.BatteryChargeStatus != BatteryChargeStatus.NoSystemBattery)
{
    var batteryStatus = powerStatus.BatteryChargeStatus.ToString()
}

From my test systems I get a wide variety of results such as:

High,

Low,

Charging,

High Charging

Low, Charging

Low, Critical

and heres the strange one... 0?

I'd imagine that it has something to do with BatteryChargeStatus Enum

JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • Your battery is flat:) – Martin James Sep 30 '14 at 15:10
  • Well why don't you examine the actual value being returned, without the ToString()? – RenniePet Sep 30 '14 at 15:11
  • 1
    ... that's some brilliant flags values there. Unknown includes all of the other status flags! – Rawling Sep 30 '14 at 15:17
  • @Rawling I don't understand what you're getting at? – JKennedy Sep 30 '14 at 15:19
  • The value is presumably set by the manufacturer in accordance to Microsoft's specifications, and this particular manufacturer messed up. – RenniePet Sep 30 '14 at 15:20
  • 1
    What @Rawling is talking about is that flag-style enums should only have values that are powers of two. So 10 = 2 + 8 = low + charging. But the value 255 is crazy for a flags-style enum - someone at Microsoft made a mistake in specifying that value for "unknown". – RenniePet Sep 30 '14 at 15:21
  • ahh I see. So I guess this value is a manufacturers mistake and there's not a lot I can do about it apart from maybe write something to catch the case when its 0 and assign it to my own string value – JKennedy Sep 30 '14 at 15:25

1 Answers1

4

They missed one. From the docs of the underlying SYSTEM_POWER_STATUS operating system declaration:

The value is zero if the battery is not being charged and the battery capacity is between low and high

So just make up your own, like:

    var status = SystemInformation.PowerStatus.BatteryChargeStatus;
    if (status != BatteryChargeStatus.NoSystemBattery) {
        var batteryStatus = status == 0 ? "Not charging" : status.ToString();
        // etc...
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • OK, so my comment above about the manufacturer having messed up is in error. This answer makes it clear that the problem is that the enum definition that should reflect the underlying SYSTEM_POWER_STATUS operating system declaration is missing a value. That on top of the underlying flags being very poorly specified - both zero and 255 are stupid values to use for flags. – RenniePet Sep 30 '14 at 15:33