【问题标题】:Battery Status in winformswinforms中的电池状态
【发布时间】:2016-08-16 16:49:53
【问题描述】:

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

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

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 statement 将电池状态从 100 调用到 1

或者按照我的做法,我必须再做 99 次if statements

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

Charge Rate
Discharge Rate
Remaining Capacity
Voltage

我一直在if statements 使用电池状态标签。在我开始做 99 次以上if statements 之前,我想确定没有更简单的方法吗?

****************更新******** 我想到了。感谢提供帮助的人的帮助。

【问题讨论】:

  • 你为什么认为你需要?
  • 我一直在标签上使用if statements。我从来没有被展示过任何其他方式,我研究过的大多数例子都是用if statements 来做的,在过去的 5 年里,我都是这样做的。我想知道是否有更简单的方法
  • 这里根本没有理由声明if...除非您在特定情况下调用BatteryStatus(我什至无法理解)。
  • 此外,当您有多个电池时,逻辑是有问题的。您只会在列表中最后一块电池上获得剩余电量。
  • 我刚刚做了label13.Text = estimatedChargeRemaining.ToString(); 并且它有效。但是我认为这会导致一个问题,说当电池电量达到 10% 时,我如何在不使用 if statements 的情况下将 10% 的文本更改为红色

标签: c# winforms winapi perfmon


【解决方案1】:

我觉得你想做的是这样的:

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 声明,无论百分比是多少,标签都会更新。

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

  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;
    }
}

【讨论】:

  • 好吧 label13.Text = estimatedChargeRemaining.ToString(); 我可以使用 label13.ForeColor = Color.Red; 但将其用作 if 语句
  • 此代码以及其他帖子和 OP 不会仅返回 allBatteries 中最后一个电池的数据。顺便说一句,Case 语句对于多个级别来说是一种简单的代码方法。
  • 会,但是什么样的设备有多个电池?可能有方法返回一个包含 1 个元素的数组。
【解决方案2】:
 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.ToString() + "  " + "%";
            if (estimatedChargeRemaining < 15 )
            {
                label13.ForeColor = Color.Red;
            }
        }



    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多