【问题标题】:Find out battery charge capacity in percentage using C# or .NET使用 C# 或 .NET 找出电池充电容量的百分比
【发布时间】:2012-02-15 06:55:36
【问题描述】:

我有一个获取详细系统信息的应用程序,并且我能够获取剩余电量的百分比,但不能获取电池本身的百分比。

说明:随着时间的推移,电池会慢慢变得越来越差,一些应用程序(例如笔记本电脑上的戴尔支持中心)会显示电池状态,而不是充电状态,过去是 100%,但现在是 90%。

如何在 C# 或 .NET 中获取此值?

【问题讨论】:

  • “在你投票结束这个之前,请先看看这个问题。”大概没有人会在不先阅读之前投票结束您的问题。
  • @ean5533:实际上,我有时也遇到过这种情况。

标签: c# .net power-management


【解决方案1】:

不需要不必要的复杂的事情。尝试类似:

using System.Management;

PowerStatus pwr = SystemInformation.PowerStatus;

String strBatteryChargingStatus;
strBatteryChargingStatus = pwr.BatteryChargeStatus.ToString();
MessageBox.Show("battery charge status : " + batterystatus);

String strBatterylife;
strBatterylife = pwr.BatteryLifePercent.ToString();
MessageBox.Show("Battery life: "+batterylife);

这样就可以获得所有的电池信息。

【讨论】:

  • SystemInformation 不在System.Management 命名空间,而是在System.Windows.Forms
  • @blade 是对的,这篇文章具有误导性,如果它不引用表单,它就不适用于控制台应用程序......
【解决方案2】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace batterie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            showbattrie();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void showbattrie()
        {
            PowerStatus status = SystemInformation.PowerStatus;
            textBox1.Text = status.BatteryLifePercent.ToString("P0");
        }
    }
}

【讨论】:

    【解决方案3】:

    您似乎正在寻找FullChargeCapacityDesignCapacityCurrentCapacity 的值。作为以前解决过这个问题的人,让我做几个cmets。

    通常采用的第一条路由是通过 WMI 查询 (Win32_Battery)。然而,在我运行 WMI 查询 (Win32_Battery) 的测试笔记本电脑上,其中包括多个制造商,我一直遇到FullChargeCapacity 总是返回零的问题。由于这不起作用,我使用 Win32 API 重新编写了我的解决方案,并成功地通过这种方式获得了准确的值。

    希望 WMI 能为您工作。但是,如果您遇到与我相同的问题,这里是 Win32API 所需步骤的摘要。

    • 使用SetupDiGetClassDevs 获取电池的设备句柄 (GUID_DEVCLASS_BATTERY)。

    • 使用SetupDiEnumDeviceInterfaces获取设备数据(SP_DEVICE_INTERFACE_DATA)。

    • 使用SetupDiGetDeviceInterfaceDetail获取设备路径(SP_DEVICE_INTERFACE_DETAIL_DATA)。

    • 使用CreateFile 和设备路径来获取电池的句柄。

    • 使用DeviceIoControl 和电池手柄,IOCTL_BATTERY_QUERY_TAG 检索电池查询信息 (BATTERY_QUERY_INFORMATION)。

    • 使用DeviceIoControl 和电池手柄,IOCTL_BATTERY_QUERY_INFORMATION 和编组structs 来检索电池信息 (BATTERY_INFORMATION)。

    另请参阅 MSDN 上的 Enumerating Battery Devices 帖子,因为我发现这很有帮助。

    如有必要,我可以发布我的解决方案,但使用所有本机 struct 定义,它最终需要大约 500 行代码。

    示例源代码:https://gist.github.com/ahawker/9715872

    【讨论】:

    • 如果您能发布您的解决方案将会非常有帮助。
    【解决方案4】:
    PowerStatus p = SystemInformation.PowerStatus;
    int a = (int)(p.BatteryLifePercent * 100);
    MessageBox.Show(""+a);
    

    【讨论】:

      【解决方案5】:
      BatteryChargeStatus.Text = SystemInformation.PowerStatus.BatteryChargeStatus.ToString(); 
      BatteryFullLifetime.Text = SystemInformation.PowerStatus.BatteryFullLifetime.ToString();    
      BatteryLifePercent.Text = SystemInformation.PowerStatus.BatteryLifePercent.ToString(); 
      BatteryLifeRemaining.Text = SystemInformation.PowerStatus.BatteryLifeRemaining.ToString(); 
      PowerLineStatus.Text = SystemInformation.PowerStatus.PowerLineStatus.ToString();
      

      如果您想执行一些操作,只需将这些字符串值转换为整数即可。

      【讨论】:

        【解决方案6】:

        WMI 为我工作(在 3 个不同品牌的笔记本上测试),但我不得不使用这样的东西:

        new ManagementObjectSearcher(@"\\localhost\root\wmi", 
                "Select FullChargedCapacity From BatteryFullChargedCapacity");
        // use value of resultingInstance.Properties["FullChargedCapacity"]
        
        new ManagementObjectSearcher(@"\\localhost\root\wmi",
                "Select DesignedCapacity From BatteryStaticData");
        //use value of resultingInstance2.Properties["DesignedCapacity"]
        

        【讨论】:

          【解决方案7】:

          在 C# 中获取电池电量的简单代码

          protected void batteryLevel ()
          {
              var filter  = new IntentFilter(Intent.ActionBatteryChanged);
              var battery = RegisterReceiver(null, filter);
              int level   = battery.GetIntExtra(BatteryManager.ExtraLevel, -1);
              int scale   = battery.GetIntExtra(BatteryManager.ExtraScale, -1);
          
              double level_0_to_100 = Math.Floor (level * 100D / scale);
          
          } 
          

          【讨论】:

          【解决方案8】:

          没有要测试的笔记本电脑,但我猜你可以使用 WMI 类 Win32_Battery

          它有两个看起来很有趣的字段 - DesignCapacity,它告诉你

          电池的设计容量,以毫瓦时为单位。

          FullChargeCapacity,其中有一个引人入胜的注释

          电池的完全充电容量,以毫瓦时为单位。该值与 DesignCapacity 属性的比较决定了何时需要更换电池。

          所以我的猜测是,你可以使用 WMI 读取这两个值,然后计算FullChargeCapacity/DesignCapacity 以找到电池健康百分比数字。

          编辑

          这是一个使用 C# 访问 WMI 信息的简短示例。我首先添加了对System.Management 程序集的引用。那么:

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Management;
          
          namespace ConsoleApplication1
          {
              class Program
              {
                  static void Main(string[] args)
                  {
                      System.Management.ObjectQuery query = new ObjectQuery("Select * FROM Win32_Battery");
                      ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
          
                      ManagementObjectCollection collection = searcher.Get();
          
                      foreach (ManagementObject mo in collection)
                      {
                          foreach (PropertyData property in mo.Properties)
                          {
                              Console.WriteLine("Property {0}: Value is {1}", property.Name, property.Value);
                          }                   
                      }
                  }
              }
          }
          

          另外,请注意,您基本上是针对 WMI 运行类似 SQL 的查询,因此您可以根据需要进行更改。 Windows Management Instrumentation Query LanguageWQL 是您想要搜索以了解更多信息的内容。

          另外看看 ahawker 的回答,如果 WMI 没有正确捕获电池数据,它可能最终会更有帮助,正如他所指出的那样。

          【讨论】:

          • 如何在我的 C# 应用程序中实际使用它?
          • @HBellamy,我添加了一个访问 WMI 的示例,如果有帮助请告诉我。
          • 现在,如果只有 Metro 应用程序可以使用它... :(
          • @Shaamaan 确实如此。不知道微软为什么以这种方式限制他们的 API。
          【解决方案9】:

          您可以使用 System.Windows.Forms.PowerStatus 类 - http://msdn.microsoft.com/en-us/library/system.windows.forms.powerstatus.aspx

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-26
            • 2023-04-07
            • 1970-01-01
            • 1970-01-01
            • 2016-11-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多