1

我试图在 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

任何帮助将不胜感激。

4

1 回答 1

1

尝试改用BatteryStatusWMI 类。

试试这个样本。

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();
        }
    }
}
于 2013-07-15T16:25:59.093 回答