更多资源:http://denghejun.github.io
[DllImport("kernel32.dll",EntryPoint="GetSystemPowerStatus")] //win32 api
private static extern void GetSystemPowerStatus(ref SYSTEM_POWER_STATUS lpSystemPowerStatus);
public struct SYSTEM_POWER_STATUS //结构体
{
public Byte ACLineStatus; //0 = offline, 1 = Online, 255 = UnKnown Status.
public Byte BatteryFlag;
public Byte BatteryLifePercent;
public Byte Reserved1;
public int BatteryLifeTime;
public int BatteryFullLifeTime;
}
/// <summary>
/// 获取系统电量百分比
/// </summary>
/// <returns></returns>
public static float GetSystemPower()
{
SYSTEM_POWER_STATUS SysPower = new SYSTEM_POWER_STATUS();
GetSystemPowerStatus(ref SysPower);
return SysPower.BatteryLifePercent;
}