2013-06-12 52 views
0

我在C#中的电池信息提取有问题。寻找:
电池信息c#

  • 设计能力
  • 满充容量
  • 电流容量
  • 警戒级别
  • 关键偏置
  • 费率

的其余信息提取从SystemInformation.PowerStatusSELECT from CIM_Battery类。

不再支持此类的许多属性的问题。

我应该在哪里查找其余的信息?

+5

什么设备的黄油状态? Hanheld,笔记本电脑? –

+0

http://stackoverflow.com/questions/8945986/find-out-battery-status-in-c-sharp-or-net – Kobunite

+0

对于一个普通的笔记本电脑。 – Andrzej

回答

0

您可以使用Win32_Battery类,here是一个示例,this是MSDN上的Win32_Battery类。

+0

不支持Win32_Battery类中的大部分信息。 – Andrzej

0

对于手持设备,您可以使用该课程。至少我使用它多年。

using System.Runtime.InteropServices; 
using System; 

namespace uti { 
public class Power { 

    [DllImport("coredll.dll", EntryPoint = "KernelIoControl", SetLastError = true)] 
    private static extern bool KernelIoControl(int dwIoControlCode, System.IntPtr lpInBuf, int nInBufSize, byte[] lpOutBuf, int nOutBufSize, ref int lpBytesReturned); 

    private const int FILE_ANY_ACCESS = 0; 
    private const int FILE_DEVICE_HAL = 257; 
    private const int METHOD_BUFFERED = 0; 
    private const int IOCTL_HAL_REBOOT = (FILE_DEVICE_HAL << 16) | (FILE_ANY_ACCESS << 14) | (15 << 2) | METHOD_BUFFERED; 

    public static bool DeviceReset() { 
     int cb = 0; 
     return KernelIoControl(IOCTL_HAL_REBOOT, System.IntPtr.Zero, 0, null, 0, ref cb); 
    } 


    private struct SYSTEM_POWER_STATUS_EX { 
     public byte ACLineStatus; 
     public byte BatteryFlag; 
     public byte BatteryLifePercent; 
     public byte Reserved1; 
     public uint BatteryLifeTime; 
     public uint BatteryFullLifeTime; 
     public byte Reserved2; 
     public byte BackupBatteryFlag; 
     public byte BackupBatteryLifePercent; 
     public byte Reserved3; 
     public uint BackupBatteryLifeTime; 
     public uint BackupBatteryFullLifeTime; 
    } 

    [DllImport("coredll.dll")] 
    extern static private bool GetSystemPowerStatusEx(ref SYSTEM_POWER_STATUS_EX sps); 

    public enum enACLineStatus { 
     Offline = 0, 
     Online = 1, 
     Backuppower = 2, 
     Unknownstatus = 255 
    } 

    [FlagsAttribute] 
    public enum enBatteryFlag { 
     High = 1, 
     Low = 2, 
     Critical = 4, 
     Charging = 8, 
     Nosystembattery = 128, 
     Unknownstatus = 255 
    } 


    static enACLineStatus _ACLineStatus = 0; 

    public static enACLineStatus ACLineStatus { 
     get { 
      return _ACLineStatus; 
     } 
    } 

    static enBatteryFlag _BatteryFlag = 0; 

    public static enBatteryFlag BatteryFlag { 
     get { 
      return _BatteryFlag; 
     } 
    } 

    static byte _BatteryLifePercent = 0; 

    public static byte BatteryLifePercent { 
     get { 
      return _BatteryLifePercent; 
     } 
    } 

    static byte _Reserved1 = 0; 

    public static byte Reserved1 { 
     get { 
      return _Reserved1; 
     } 
    } 

    static uint _BatteryLifeTime = 0; 

    public static uint BatteryLifeTime { 
     get { 
      return _BatteryLifeTime; 
     } 
    } 

    static uint _BatteryFullLifeTime = 0; 

    public static uint BatteryFullLifeTime { 
     get { 
      return _BatteryFullLifeTime; 
     } 
    } 

    static byte _Reserved2 = 0; 

    public static byte Reserved2 { 
     get { 
      return _Reserved2; 
     } 
    } 

    static enBatteryFlag _BackupBatteryFlag = 0; 

    public static enBatteryFlag BackupBatteryFlag { 
     get { 
      return _BackupBatteryFlag; 
     } 
    } 

    static byte _BackupBatteryLifePercent = 0; 

    public static byte BackupBatteryLifePercent { 
     get { 
      return _BackupBatteryLifePercent; 
     } 
    } 

    static byte _Reserved3 = 0; 

    public static byte Reserved3 { 
     get { 
      return _Reserved3; 
     } 
    } 

    static uint _BackupBatteryLifeTime = 0; 

    public static uint BackupBatteryLifeTime { 
     get { 
      return _BackupBatteryLifeTime; 
     } 
    } 

    static uint _BackupBatteryFullLifeTime = 0; 

    public static uint BackupBatteryFullLifeTime { 
     get { 
      return _BackupBatteryFullLifeTime; 
     } 
    } 

    public static void Refresh() { 
     SYSTEM_POWER_STATUS_EX sps = new SYSTEM_POWER_STATUS_EX(); 

     bool bRet = GetSystemPowerStatusEx(ref sps); 
     if (!bRet) 
      return; 

     _ACLineStatus = 
      (enACLineStatus) 
      sps.ACLineStatus; 
     _BatteryFlag = 
      (enBatteryFlag) 
      sps.BatteryFlag; 
     _BatteryLifePercent = 
      sps.BatteryLifePercent; 
     _Reserved1 = 
      sps.Reserved1; 
     _BatteryLifeTime = 
      sps.BatteryLifeTime; 
     _BatteryFullLifeTime = 
      sps.BatteryFullLifeTime; 
     _Reserved2 = 
      sps.Reserved2; 
     _BackupBatteryFlag = 
      (enBatteryFlag) 
      sps.BackupBatteryFlag; 
     _BackupBatteryLifePercent = 
      sps.BackupBatteryLifePercent; 
     _Reserved3 = 
      sps.Reserved3; 
     _BackupBatteryLifeTime = 
      sps.BackupBatteryLifeTime; 
     _BackupBatteryFullLifeTime = 
      sps.BackupBatteryFullLifeTime; 
    } 


    [DllImport("coredll.dll", SetLastError = true)] 
    static extern int SetSystemPowerState(string psState, int StateFlags, int Options); 

    const int POWER_STATE_ON = 0x00010000; 
    const int POWER_STATE_OFF = 0x00020000; 
    const int POWER_STATE_SUSPEND = 0x00200000; 
    const int POWER_FORCE = 4096; 
    const int POWER_STATE_RESET = 0x00800000; 
    } 
} 
+0

我发现的大部分信息。 – Andrzej

+0

对你有好处Andrzej。 –

+0

但不是全部:( – Andrzej