了解电池状态,在C#或.NET [英] Find out battery status in C# or .NET

查看:160
本文介绍了了解电池状态,在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.

说明:随着时间的推移,电池慢慢会变得越来越坏,和一些应用程序,像笔记本电脑戴尔支持中心,显示电池状态,不能充电的状态,这曾经是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类<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa394074%28v=vs.85%29.aspx">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.

Full- ChargeCapacity的,其中有迷人的注意,

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来阅读这两个值,然后计算 Full- ChargeCapacity的/为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.

另外看看ahawker的回答,它可能最终会被更多的帮助,如果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天全站免登陆