c# - Windows 桌面应用程序中是否有 API 函数可以获取电池电量?

标签 c# windows visual-studio-2012

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

最佳答案

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

关于c# - Windows 桌面应用程序中是否有 API 函数可以获取电池电量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901682/

相关文章:

.net - 从 .NET Windows 服务调用 native DLL 失败

javascript - console.log() 在 Visual Studio 2012 中不起作用

visual-studio-2012 - Visual Studio远程调试-在同一个域中找不到计算机

c# - 如何在C#中实现条件封装

c# - 动态 CRM : Get metadata for statuscode/statecode mapping

windows - 在 Windows 7 上将 boost log 1_60 与 MinGw 链接时出现问题

windows - ShellIconOverlayIdentifiers - 为什么这么少?

c++ - Qt Creator编译器配置问题visual studio 2017

c# - 遇到锁时如何停止线程?

c# - 为什么我的 Label-control 不使用 CSS 样式?