我试图在 WMI 中捕获 BatteryStatusChange 事件。当我执行我的程序时,当电池开始充电或放电时,我根本没有收到任何电池事件。
这是我正在尝试的代码:
namespace BatteryStatusChange
{
class Program
{
static void Main(string[] args)
{
try
{
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM BatteryStatusChange");
ManagementScope Scope = new ManagementScope(String.Format("\\\\{0}\\root\\WMI", "localhost"), null);
Scope.Connect();
ManagementEventWatcher watcher = new ManagementEventWatcher(Scope, query);
Console.WriteLine("Waiting for an event...");
ManagementBaseObject eventObj = watcher.WaitForNextEvent();
Console.WriteLine("{0} event occurred.", eventObj["__CLASS"]);
// Cancel the event subscription
watcher.Stop();
return;
}
catch (ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
}
}
当我执行这个程序时,当电池开始充电或放电时,我根本没有收到任何电池事件。
我也尝试了稍微不同的查询字符串,如下所示,但无济于事:
SELECT * FROM BatteryStatusChange WHERE PowerOnline = TRUE
任何帮助将不胜感激。