C#判断AC电源是否开关

2019-07-13 21:48发布

class PowerStatusHelper { [DllImport("kernel32.dll")] protected static extern Boolean GetSystemPowerStatus(out SystemPowerStatus sps); protected enum ACPowerState : byte { Offline = 0, Online = 1, Unknown = 255 } protected enum BatteryFlag : byte { High = 1, Low = 2, Critical = 4, Charging = 8, NoSystemBattery = 128, Unknown = 255 } protected struct SystemPowerStatus { public ACPowerState LineStatus; public BatteryFlag _BatteryFlag; public Byte _BatteryLifePercent; public Byte _Reserved1; public Int32 _BatteryLifeTime; public Int32 _BatteryFullLifeTime; } public static bool CheckIfACOn() { SystemPowerStatus sysPowerStatus = new SystemPowerStatus(); GetSystemPowerStatus(out sysPowerStatus); return ACPowerState.Online == sysPowerStatus.LineStatus; } }
CheckIfACOn()调用这个方法,返回结果true则电源连接,反之亦然