使用C#中的系统管理的电池电量

时间:2013-08-08 11:05:37

标签: c# speech-recognition batterylevel

所以我正在用C#编写一个语音识别程序,我编译了几行代码speaks back the current battery level当我说"battery Level".时只问题是,它不起作用。

调试阶段它构建正常,没有错误或警告但当我说“电池电量”时我没有得到任何回应。

if (e.Result.Text == "battery level")
        {
            System.Management.ManagementClass wmi = new System.Management.ManagementClass("Win32_Battery");
            var allBatteries = wmi.GetInstances();
            String estimatedChargeRemaining = String.Empty;

            foreach (var battery in allBatteries)
            {
                estimatedChargeRemaining = Convert.ToString(battery["EstimatedChargeRemaining"]);
            }

            JARVIS.Speak("Estimated Charge Remaining: " + estimatedChargeRemaining + "%");
            return;
        }

是否有人发现代码中存在明显错误,可能导致其无法正常工作?

感谢。

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些可能会或可能不会影响您的应用程序的错误

if (e.Result.Text.ToLower() == "battery level")   //First Change
{
    System.Management.ManagementClass wmi = new System.Management.ManagementClass("Win32_Battery");
    var allBatteries = wmi.GetInstances();
    String estimatedChargeRemaining = String.Empty;

    foreach (var battery in allBatteries)
    {
       estimatedChargeRemaining = Convert.ToString(battery["EstimatedChargeRemaining"]);
       JARVIS.Speak("Estimated Charge Remaining: " + estimatedChargeRemaining + "%");  //second change as you may have more than one batteries.
    }       
    return;
}