C#:获取设备电量相关信息

更多资源: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;
}
上一篇:Java笔记9-正则表达式


下一篇:Get vertical scrollbar width and example