winforms中的电池状态 [英] Battery Status in winforms

查看:26
本文介绍了winforms中的电池状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 perfmon 创建一个 winform 应用程序.我发现电池状态不起作用,因为它是 Windows 管理的一部分.所以我决定走wmi路线.

I have been creating a winform application using perfmon. I have found out that the battery status will not work because its part of windows management. So I decide to go the wmi route.

所以我的问题是当我将电池状态放在如下所示的标签中时:

So my question is when I put the battery status in a label as shown below:

private void BatteryStatus()
    {
        System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
        var allBatteries = wmi.GetInstances();

        foreach (var battery in allBatteries)
        {
            int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]);
            if (estimatedChargeRemaining == 100)
            {
                label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "%";
            }
        }



    }

剩余电量显示完美.我的问题是,有没有一种方法可以让我只用一个 if 语句 将电池状态从 100 调用到 1

The charge remaining is shown perfectly. My question is, is there a way that I can have just one if statement to call the battery status from 100 to 1

或者我这样做的方式是否需要再做 99 个 if 语句?

or the way I am doing it will I have to do 99 more if statements?

这是我自定义构建的性能监视器的一部分.如果 perfmon 允许计数器,它会更容易.这是我能想到的获取电池统计信息的唯一方法,例如:

this is part of my performance monitor I am custom building. It would be easier if perfmon would allow the counter. This is the only way I can think of to get the battery stats such as:

Charge Rate
Discharge Rate
Remaining Capacity
Voltage

我总是用电池状态的标签做if 语句.在我再做 99 个 if 语句 之前,我想确定没有更简单的方法吗?

I have always did if statements with the labels on battery status. Before I go into doing 99 more if statements I want to be sure there is not an easier way?

*********** 更新 ************我想到了.感谢帮助过的人.

*********** Update ************ I figured it out. Thanks for the help for the ones who helped.

推荐答案

我想要做的是:

private void BatteryStatus()
{
    System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
    var allBatteries = wmi.GetInstances();

    foreach (var battery in allBatteries)
    {
        int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]);           
        label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "%";
    }
}

不需要和if语句,无论百分比是多少,标签都会更新.

No need for and if statment, the label will be updated no matter what the percentage is.

在问题的第二部分,您说要显示电池状态",然后可以像这样使用 if:

On the second part of the question you say that you want to show the "battery status", you can then use if like this:

  private void BatteryStatus()
 {
    System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
    var allBatteries = wmi.GetInstances();

    foreach (var battery in allBatteries)
    {
        int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]); 
        string Status = "";    
        if(estimatedChargeRemaining < 15) Status = "Critical";
        else  if(estimatedChargeRemaining < 35) Status = "Low";
        else  if(estimatedChargeRemaining < 60) Status = "Regular";
        else  if(estimatedChargeRemaining < 90) Status = "High";
        else Status = "Complete";

        label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "% | Status: " + Status;
    }
}

这篇关于winforms中的电池状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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