使用C#或.NET找出电池充电容量的百分比 [英] Find out battery charge capacity in percentage using C# or .NET

查看:490
本文介绍了使用C#或.NET找出电池充电容量的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that gets detailed system information, and I have been able to get the percent of charge remaining but not the percent of the battery itself.

说明:随着时间的流逝,电池的速度越来越慢,某些应用程序(例如笔记本电脑上的Dell支持中心)显示的是电池状态,而不是电量状态,以前是100%,但现在是90%

Explanation: As time goes on, the battery slowly gets worse and worse, and some applications, like Dell Support Center on Laptops, display the status of the battery, not the status of the charge, which used to be 100% but now is 90%.

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

How can I get this value in C# or .NET?

推荐答案

没有便携式计算机可以测试,但是我猜您可以使用WMI类 Win32_Battery

Don't have a laptop to test with, but I'm guessing you could use the WMI class Win32_Battery.

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

It has two fields that look interesting - DesignCapacity, which tells you


电池的设计容量(以毫瓦小时为单位)。

Design capacity of the battery in milliwatt-hours.

FullChargeCapacity , ich令人着迷的是,

and FullChargeCapacity, which has the fascinating note that


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

Full charge capacity of the battery in milliwatt-hours. Comparison of the value to the DesignCapacity property determines when the battery requires replacement.

所以我的猜测是您可以使用WMI读取这两个值,然后计算 FullChargeCapacity / DesignCapacity 查找电池运行状况百分比数字。

So my guess is that you can use WMI to read these two values, and then calculate FullChargeCapacity/DesignCapacity to find the battery health percentage number.

编辑

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

Here's a brief example of accessing WMI information using C#. I first added a reference to the System.Management assembly. Then:

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管理规范查询语言 WQL ,以了解更多信息。

Also, note that you are basically running a SQL-like query against WMI, so you can vary that if you want. Windows Management Instrumentation Query Language, or WQL, is what you want to search for to learn more about it.

也请看看小贩的答案,如WMI所述,如果WMI无法正确捕获电池数据,最终可能会更有帮助。

Also take a look at ahawker's answer, it may end up being more helpful if WMI isn't properly capturing the battery data, as he notes.

这篇关于使用C#或.NET找出电池充电容量的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆