Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#或.NET查找电池充电容量的百分比_C#_.net_Power Management - Fatal编程技术网

使用C#或.NET查找电池充电容量的百分比

使用C#或.NET查找电池充电容量的百分比,c#,.net,power-management,C#,.net,Power Management,我有一个应用程序可以获取详细的系统信息,我可以得到剩余电量的百分比,但不能得到电池本身的百分比 说明:随着时间的推移,电池会慢慢变差,一些应用程序(如笔记本电脑上的Dell Support Center)会显示电池的状态,而不是充电状态,以前是100%,但现在是90% 如何在C#或.NET中获取此值?您可以使用System.Windows.Forms.PowerStatus类-没有笔记本电脑可供测试,但我猜您可以使用WMI类 它有两个看起来很有趣的字段-DesignCapacity,它告诉您 电

我有一个应用程序可以获取详细的系统信息,我可以得到剩余电量的百分比,但不能得到电池本身的百分比

说明:随着时间的推移,电池会慢慢变差,一些应用程序(如笔记本电脑上的Dell Support Center)会显示电池的状态,而不是充电状态,以前是100%,但现在是90%


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

您可以使用System.Windows.Forms.PowerStatus类-

没有笔记本电脑可供测试,但我猜您可以使用WMI类

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

电池的设计容量(毫瓦时)

全充电容量
,其中有一个引人入胜的特点:

电池的完全充电容量(毫瓦时)。将该值与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查询语言或
WQL
,是您要搜索以了解更多信息的语言


还请看一看ahawker的答案,正如他所指出的,如果WMI没有正确捕获电池数据,它可能会更有帮助。

您似乎在寻找
满充电容量
设计容量
当前容量
的值。作为一个曾经解决过这个问题的人,让我发表一些评论

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

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

  • 使用
    SetupDiGetClassDevs
    获取电池的设备句柄(
    GUID\u DEVCLASS\u电池

  • 使用
    setupDienumDeviceInterface
    获取设备数据(
    SP_设备_接口_数据

  • 使用
    SetupDiGetDeviceInterfaceDetail
    获取设备路径(
    SP\u设备\u接口\u细节\u数据

  • 在设备路径中使用
    CreateFile
    ,以获取电池的手柄

  • 使用带有电池手柄的
    DeviceIoControl
    IOCTL\u battery\u QUERY\u标签
    检索电池查询信息(
    battery\u QUERY\u INFORMATION

  • 使用带电池手柄的
    DeviceIoControl
    IOCTL\u battery\u QUERY\u INFORMATION
    和编组的
    structs
    检索电池信息(
    battery\u INFORMATION

还可以看到MSDN上的帖子,因为我发现这很有帮助

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


示例源代码:

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

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"]

不需要把不必要的事情复杂化。尝试以下方法:

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);
通过这种方式,您可以获得所有电池信息

PowerStatus p = SystemInformation.PowerStatus;
int a = (int)(p.BatteryLifePercent * 100);
MessageBox.Show(""+a);

如果要执行某些操作,只需将这些字符串值转换为整数。

“在投票结束此操作之前,请先看一看问题。”可能没有人会投票结束您的问题而不先阅读。@ean5533:我有时也会遇到这种情况,事实上。我如何在我的C#应用程序中实际使用它?@HBellamy,我添加了一个访问WMI的示例,请告诉我它是否有帮助。现在,如果metro应用程序中有此功能的话…:(@Shaamaan确实如此。我不知道微软为什么要以这种方式限制他们的API。如果你能发布你的解决方案,那将非常有帮助。SystemInformation不在
System.Management
命名空间中,而是在
System.Windows.Forms
@blade是正确的,这篇文章是误导性的,如果它不引用rms…这是针对Android(不是Windows)的。代码使用Mono.Android.dll:例如,和。
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");
        }
    }
}
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();
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");
        }
    }
}