如何在C#中使用WMI事件

时间:2012-07-28 16:28:27

标签: c# events wmi

有一些事件通过WMI公开,但我似乎无法找到订阅和提醒这些事件的任何示例。特别是我想实现WmiMonitorBrightnessEvent向Growl / Snarl推送通知。

1 个答案:

答案 0 :(得分:12)

这是用于接收WmiMonitorBrightnessEvent 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("Active :          " + e.NewEvent.Properties["Active"].Value.ToString());
            Console.WriteLine("Brightness :      " + e.NewEvent.Properties["Brightness"].Value.ToString());
            Console.WriteLine("InstanceName :    " + e.NewEvent.Properties["InstanceName"].Value.ToString());

        }

        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 WmiMonitorBrightnessEvent";

                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}", "WmiMonitorBrightnessEvent");
           Console.WriteLine("Press Enter to exit");
           EventWatcherAsync eventWatcher = new EventWatcherAsync();
           Console.Read();
        }
    }
}

如果您是WMI新手,请尝试使用WMI Delphi Code Creator之类的工具,并阅读与此主题相关的文档Receiving a WMI Event