是否有任何 API 函数可以在 Windows 桌面应用程序中获取电池电量? [英] Is there any API function to get the battery level in Windows Desktop Apps?

查看:33
本文介绍了是否有任何 API 函数可以在 Windows 桌面应用程序中获取电池电量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个桌面应用程序,我想在其中获取我的设备(笔记本电脑)的电池电量.我为此使用 C#.有什么方法可以帮助我做到这一点吗?我已经使用 Windows.Phone.Devices.Power.Battery.GetDefault().RemainingChargePercent 在 Windows Phone 8 平台上完成了这项工作.桌面端也有类似的API吗?

I'm creating a desktop app where I want to get the battery level of my device (laptop). I am using C# for this. Is there any method which would help me do this? I've done this on the Windows Phone 8 platform using the Windows.Phone.Devices.Power.Battery.GetDefault().RemainingChargePercent. Is there any similar API on the desktop side too?

推荐答案

private static string GetBatteryLevel()
{
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Battery");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
    {
        using (ManagementObjectCollection results = searcher.Get())
        {
            using (var enumerator = results.GetEnumerator())
            {
                if (!enumerator.MoveNext())
                    return string.Empty;

                return string.Format("{1} {0}%", enumerator.Current["EstimatedChargeRemaining"],
                                     enumerator.Current["BatteryStatus"].ToString() == "1"
                                         ? string.Empty
                                         : "Charging:");
            }
        }
    }
}

您可以使用Windows Management Instrumentation

这篇关于是否有任何 API 函数可以在 Windows 桌面应用程序中获取电池电量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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