2013-07-15 72 views
1

我試圖捕獲WMI中的BatteryStatusChange事件。當我執行我的程序時,無論是電池開始充電還是放電,我都沒有收到任何電池事件。

這是我的代碼,我嘗試:BatteryStatusChange未收到WMI事件

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 

任何幫助將不勝感激。

回答

1

請嘗試使用BatteryStatus WMI類。

試試這個例子。

using System; 
using System.Collections.Generic; 
using System.Management; 
using System.Text; 


namespace GetWMI_Info 
{ 
    public class EventWatcherAsync 
    { 
     private void WmiEventHandler(object sender, EventArrivedEventArgs e) 
     { 
      Console.WriteLine("TargetInstance.Charging :    " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Charging"]); 
      Console.WriteLine("TargetInstance.Description :   " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Description"]); 
      Console.WriteLine("TargetInstance.Name :     " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]); 
      Console.WriteLine("TargetInstance.PowerOnline :   " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["PowerOnline"]); 
      Console.WriteLine("TargetInstance.RemainingCapacity : " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["RemainingCapacity"]); 
      Console.WriteLine("TargetInstance.Voltage :    " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Voltage"]); 

     } 

     public EventWatcherAsync() 
     { 
      try 
      { 
       string ComputerName = "localhost"; 
       string WmiQuery; 
       ManagementEventWatcher Watcher; 
       ManagementScope Scope; 


       if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
       { 
        ConnectionOptions Conn = new ConnectionOptions(); 
        Conn.Username = ""; 
        Conn.Password = ""; 
        Conn.Authority = "ntlmdomain:DOMAIN"; 
        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\WMI", ComputerName), Conn); 
       } 
       else 
        Scope = new ManagementScope(String.Format("\\\\{0}\\root\\WMI", ComputerName), null); 
       Scope.Connect(); 

       WmiQuery ="Select * From __InstanceModificationEvent Within 1 "+ 
       "Where TargetInstance ISA 'BatteryStatus' "; 

       Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery)); 
       Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler); 
       Watcher.Start(); 
       Console.Read(); 
       Watcher.Stop(); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace); 
      } 

     } 

     public static void Main(string[] args) 
     { 
      Console.WriteLine("Listening {0}", "__InstanceModificationEvent"); 
      Console.WriteLine("Press Enter to exit"); 
      EventWatcherAsync eventWatcher = new EventWatcherAsync(); 
      Console.Read(); 
     } 
    } 
} 
+0

這太好了。感謝您的回答,併爲延遲接受這個答案而表示歉意。 – Sandman

+0

沒問題。很高興幫助你。 – RRUZ