Using WMI to Monitor for USB IrDA Devices

I decided to play around with WMI to see if I could monitor the insertion and removal of a USB IrDA device as well as scan for any that may be attached when the application is started. Below is the code I used to get it to work. The next step would be to figure out how to send messages to it ;).

using System;





using System.Management;


UsbInfraredHelper {

     private ManagementEventWatcher watcher;

     public UsbInfraredHelper() { }

     public void Scan() {

          WqlObjectQuery objectQuery = new WqlObjectQuery("Select * from CIM_InfraredController");

          ManagementScope scope = new ManagementScope("root\cimv2");

          ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objectQuery);

          StringBuilder sb = new StringBuilder();

          int count = 0;

          foreach (ManagementObject managementObject in searcher.Get()) {

               string deviceId = managementObject.Properties["DeviceId"].Value as string;


String.Format("Controller #{0}: {1}", ++count, deviceId));


          MessageBox.Show((sb.Length == 0) ? "No devices found" : sb.ToString());


     public void StartMonitor() {

          WqlEventQuery eventQuery;

          ManagementOperationObserver observer = new ManagementOperationObserver();

          ManagementScope scope = new ManagementScope("root\CIMV2");

          scope.Options.EnablePrivileges =


          try {

               eventQuery =

new WqlEventQuery();

               eventQuery.EventClassName =


               eventQuery.WithinInterval =

new TimeSpan(0, 0, 3);

               eventQuery.Condition =

"TargetInstance ISA ‘Win32_InfraredDevice’";

               watcher = new ManagementEventWatcher(scope, eventQuery);

               watcher.EventArrived +=

new EventArrivedEventHandler(MonitoredEventHandler);


               MessageBox.Show("Observing changes to Infrared connection status");

          } catch (Exception ex) {




     public void MonitoredEventHandler(object sender, EventArrivedEventArgs e) {

          //Get the Event object and display its properties (all)

          foreach (PropertyData propertyData in e.NewEvent.Properties) {

               ManagementBaseObject mbo = null;

               StringBuilder sb = new StringBuilder();

               if ((mbo = propertyData.Value as ManagementBaseObject) != null) {

                    string deviceId = mbo.Properties["DeviceId"].Value as string;

                    string statusInfo = mbo.Properties["StatusInfo"].Value as string;

                    MessageBox.Show(String.Format("{0} – {1}", deviceId, statusInfo));




     public void StopMonitor() {

          if (watcher != null) {



          MessageBox.Show("No longer monitoring for USB Infrared devices.");



One thought on “Using WMI to Monitor for USB IrDA Devices

  1. Hi, J,This one looks pretty good right now!!P

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s