使用C#中的系统管理的电池电量
本文关键字:系统管理 使用 | 更新日期: 2023-09-27 18:27:02
所以我正在用C#编写一个语音识别程序,当我说"battery Level".
时,我已经编译了几行speaks back the current 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;
}
有人注意到代码中有任何明显的错误可能会阻止它工作吗
谢谢。
您的代码中有几个错误,可能会也可能不会影响您的应用程序
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;
}