C#技术如何确定计算机是否正在使用电池电源?

4
我找到了一些API,可以帮助确定电量(百分比、充电估计等),主要是在WMI中。
在我的应用程序中,我想知道计算机当前是否通过电池供电,而不是电池的状态。简而言之,在未连接电源时,我想要特殊的行为。
是否有这样的API?如果需要,我可以使用win32 API进行Pinvoke。
2个回答

2

试试这个:

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

还要检查SystemInformation.PowerStatus属性


1
System.Windows.Forms 命名空间中,有一个 SystemInformation.PowerStatus.BatteryChargeStatus 枚举可以使用:
switch (SystemInformation.PowerStatus.BatteryChargeStatus)
{
    case BatteryChargeStatus.Charging:
         break; //do nothing
    default:
        //code here. The battery isn't charging so it isn't plugged in
}

就是这样


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接