C# technique for determining if a computer is operating on battery power? [duplicate]

StackOverflow https://stackoverflow.com/questions/19070330

  •  29-06-2022
  •  | 
  •  

문제

I've found a number of API's that can help determine how much charge (percentages, charge estimates, etc) - mostly in WMI

in my application, I'd like to know if the computer is currently powered by battery, not the status of the battery. in short, I'd like special behavior when not plugged in

is there such an API? I'm happy with a win32 API to pinvoke if needed

도움이 되었습니까?

해결책

Try this:-

Boolean x =(System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == 
       PowerLineStatus.Offline);

Also check SystemInformation.PowerStatus Property

다른 팁

In the System.Windows.Forms namespace there is a SystemInformation.PowerStatus.BatteryChargeStatus enum which you can use:

switch (SystemInformation.PowerStatus.BatteryChargeStatus)
{
    case BatteryChargeStatus.Charging:
         break; //do nothing
    default:
        //code here. The battery isn't charging so it isn't plugged in
}

That's it

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top