如何从WPF applicatioin使用WMI让电池充电状态? [英] How to get Battery charging status from WPF applicatioin using WMI?

查看:418
本文介绍了如何从WPF applicatioin使用WMI让电池充电状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让充电%,而正是在Windows显示相同的文字 - 不充电,充满电,充电

I'm trying to get charging percent, and exactly the same text that is Windows showing - Not Charging, Full Charged, Charging

我使用WMI查询。

我不想硬code的文本,因为它每一次的工作不同。 有时,它显示95百分比不充电,也可以充电。

I don't want to hard-code that texts, because it's working differently every time. Sometimes it's showing 95 percent- Not Charging, or can be Charging.

有没有办法得到这一结果的文本?

Is there any way to get that result text?

我能做些什么,只是为了检查,如果它是100%的充电,显示充满电。

What I can do, just to check if it is 100 percent charged, to display Full Charged.

但对于96%,这是不同的工作,有时Windows显示带电或不充电

But for 96 percent, it is working differently, sometimes Windows display Charged, or Not Charging

推荐答案

使用的 Win32_Battery 类:

static string GetBatteryStatus() {
    ManagementScope scope = new ManagementScope("//./root/cimv2");
    SelectQuery query = new SelectQuery("Select BatteryStatus From Win32_Battery");
    using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) {
        using(ManagementObjectCollection objectCollection = searcher.Get()) {
            foreach(ManagementObject mObj in objectCollection) {
                PropertyData pData = mObj.Properties["BatteryStatus"];
                switch((Int16)pData.Value) { 
                    //...
                    case 2:return "Not Charging";
                    case 3:return "Fully Charged";
                    case 4:return "Low";
                    case 5: return "Critical";
                    //...
                }
            }
        }
    }
    return string.Empty;
}

这篇关于如何从WPF applicatioin使用WMI让电池充电状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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