确定计算机是否使用电池供电的C#技术?

确定计算机是否使用电池供电的C#技术?,c#,.net,winapi,battery,C#,.net,Winapi,Battery,我发现了一些API,它们可以帮助确定多少费用(百分比、费用估算等)——主要是在WMI中 在我的应用程序中,我想知道计算机当前是否由电池供电,而不是电池的状态。简言之,我希望在不接通电源时有特殊的行为 有这样的API吗?如果需要,我很乐意使用win32 API连接pinvoke- Boolean x =(System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Offl

我发现了一些API,它们可以帮助确定多少费用(百分比、费用估算等)——主要是在WMI中

在我的应用程序中,我想知道计算机当前是否由电池供电,而不是电池的状态。简言之,我希望在不接通电源时有特殊的行为

有这样的API吗?如果需要,我很乐意使用win32 API连接pinvoke-

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

另请检查
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
}
就这样