PowerStatus has changed... Now what?

by alski 27. January 2005 00:14
How to detect Suspend/Hibernate/Resume, battery level and Power Cord changes
After having detected that the PowerStatus has changed then what.Try this from MSDN nicely wrapped up in a class
///
/// Summary description for PowerState.
///
public class PowerStatus
{
public enum _ACLineStatus : byte
{
Offline = 0,
Online = 1,
Unknown = 255
}
public enum _BatteryFlag : byte
{
High = 1,
Low = 2,
Critical = 4,
Charging = 8,
NoSystemBattery = 128,
Unknown = 255
}
internal struct SystemPowerStatus
{
public _ACLineStatus ACLineStatus;
public _BatteryFlag BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
}

[DllImport("kernel32")]
private static extern bool GetSystemPowerStatus (out SystemPowerStatus sps);
private SystemPowerStatus _sps = new SystemPowerStatus();
public PowerStatus()
{
//Initialise the powerstate
GetSystemPowerStatus(out _sps);
}
public _ACLineStatus ACLineStatus
{
 get 
 {
  return _sps.ACLineStatus;
 }
}
public _BatteryFlag BatteryFlag
{
 get
 {
  return _sps.BatteryFlag;
 }
}
public byte BatteryLifePercent
{
 get
 {
  return _sps.BatteryLifePercent;
 }
}
public uint BatteryLifeTime
{
 get
 {
  return _sps.BatteryLifeTime;
 }
}
public uint BatteryFullLifeTime
{
 get
 {
  return _sps.BatteryFullLifeTime;
 }
}

All you need to do now is
PowerState ps = new PowerState();
if (sp.ACLine ...

etc

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

RecentComments

Comment RSS