BatteryStatusChange未收到WMI事件

时间:2013-07-15 09:13:59

标签: c# events wmi wmi-query

我正在尝试捕获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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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();
        }
    }
}