Detecting mains power

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

Does anyone know of any way of detecting whether a device has mains
power or not? I can see power state changes, but can't tell from them
whether the device is connected to a decent power supply.

(We'd like our app to keep trying to download updates if it's on mains
power, but not otherwise.)
 
Neil Cowburn said:
P/Invoke GetSystemPowerStateEx and check the value of the ACLineStatus of
the returned SYSTEM_POWER_STATUS_EX structure.

A value of 1 indicates that an AC supply is the power source. Zero indicates
battery is the power source.

Alternatively, check out GetSystemPowerStateEx in OpenNETCF.Win32.Core
(http://opennetcf.org/SourceBrowse/view.aspx?f=d:/sites/OpenNETCF/InetPub/
wwwroot/Source/OpenNETCF/Win32/Core.cs
go to Line 1627)

Aha - many thanks. Not sure why I didn't see that before. Even better,
I've just discovered the BatteryFlag enumeration too, so we can be even
more discriminating :)
 
You have to be conservative about using that. Not all devices have detailed
battery-left information available. Don't forget to handle the
BatteryUnknown case...

Paul T.
 
Back
Top