Question

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.

Était-ce utile?

La solution

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top