如何消耗在C#WMI事件 [英] How to consume WMI Events in C#

查看:580
本文介绍了如何消耗在C#WMI事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些事件通过WMI暴露,但我似乎无法找到订阅和被惊动这些事件的任何实例。特别是我想落实 WmiMonitorBrightnessEvent 来推。一个通知,低吼/咆哮

There are some events expose via WMI, but I can't seem to find any examples for subscribing and being alerted of those events. Particularly I am wanting to implement WmiMonitorBrightnessEvent to push a notification to Growl/Snarl.

推荐答案

这是收到的 WmiMonitorBrightnessEvent WMI事件。

This is a sample code for receive the WmiMonitorBrightnessEvent WMI Event.

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代码造物主 和阅读有关该主题的文档的 接听WMI事件

If you are new to the WMI try using a tool like the WMI Delphi Code Creator and reading the documentation related to this topic Receiving a WMI Event

这篇关于如何消耗在C#WMI事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆