문제

So I'm working on a speech recognition program in C# and I'v compiled a few lines of code that speaks back the current battery level when I say "battery Level". Only problem is, It doesn't work.

Debugging stage it builds fine, no errors or warnings yet when I say "battery level" I get no response.

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

Does anyone notice any obvious mistakes in the code that could prevent it from working?

Thanks.

도움이 되었습니까?

해결책

There are couple of mistakes in your code that may or may not be affecting your application

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top