Advertisement
rgelb

Detect insertion and removal of USB Sound cards

May 29th, 2013
1,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1.     using System;
  2.     using System.Management;
  3.     using Microsoft.Win32;
  4.  
  5.     public class DeviceDetector : IDisposable
  6.     {
  7.         #region Variables
  8.  
  9.         private ManagementEventWatcher insertWatcher;
  10.         private ManagementEventWatcher removeWatcher;
  11.         private readonly Guid GUID_DEVCLASS_MEDIA = new Guid("{4D36E96C-E325-11CE-BFC1-08002BE10318}");
  12.  
  13.         public delegate void DeviceInsertedHandler(object sender, DeviceChangedEventArgs eventArgs);
  14.  
  15.         public event DeviceInsertedHandler DeviceInserted;
  16.  
  17.         public delegate void DeviceRemovedHandler(object sender, DeviceChangedEventArgs eventArgs);
  18.  
  19.         public event DeviceRemovedHandler DeviceRemoved;
  20.  
  21.         #endregion
  22.  
  23.         #region Constructors
  24.  
  25.         public DeviceDetector()
  26.         {
  27.             EstablishWatchedEvents();
  28.         }
  29.  
  30.         #endregion
  31.  
  32.         #region Private Methods
  33.  
  34.         private void EstablishWatchedEvents()
  35.         {
  36.             // setup the query to monitor insertion
  37.             const string qryInsertion = "SELECT * FROM __InstanceCreationEvent " +
  38.                                         "WITHIN 2 " +
  39.                                         "WHERE TargetInstance ISA 'Win32_PnPEntity'";
  40.             insertWatcher = new ManagementEventWatcher(qryInsertion);
  41.             insertWatcher.EventArrived += insertWatcher_EventArrived;
  42.             insertWatcher.Start();
  43.  
  44.  
  45.             // setup the query to monitor removal
  46.             const string qryRemoval = "SELECT TargetInstance " +
  47.                                       "FROM __InstanceDeletionEvent " +
  48.                                       "WITHIN 2 " +
  49.                                       "WHERE TargetInstance ISA 'Win32_PnPEntity' ";
  50.  
  51.             removeWatcher = new ManagementEventWatcher(qryRemoval);
  52.             removeWatcher.EventArrived += removeWatcher_EventArrived;
  53.             removeWatcher.Start();
  54.         }
  55.  
  56.  
  57.  
  58.         private string GetDeviceName(string deviceID)
  59.         {
  60.             string[] Parts = deviceID.Split(@"\".ToCharArray());
  61.             if (Parts.Length >= 3)
  62.             {
  63.                 string DevType = Parts[0];
  64.                 string DeviceInstanceId = Parts[1];
  65.                 string DeviceUniqueID = Parts[2];
  66.                 string RegPath = @"SYSTEM\CurrentControlSet\Enum\" + DevType + "\\" + DeviceInstanceId + "\\" +
  67.                                  DeviceUniqueID;
  68.                 RegistryKey key = Registry.LocalMachine.OpenSubKey(RegPath);
  69.                 if (key != null)
  70.                 {
  71.                     object result = key.GetValue("FriendlyName");
  72.                     if (result != null)
  73.                         return result.ToString();
  74.  
  75.                 }
  76.             }
  77.             return String.Empty;
  78.         }
  79.  
  80.         #endregion
  81.  
  82.         #region Events
  83.  
  84.         private void insertWatcher_EventArrived(object sender, EventArrivedEventArgs e)
  85.         {
  86.             var mbo = (ManagementBaseObject) e.NewEvent["TargetInstance"];
  87.             if (new Guid((string) mbo["ClassGuid"]) == GUID_DEVCLASS_MEDIA)
  88.             {
  89.                 var deviceName = (string) mbo["Name"];
  90.                 if (deviceName.Length <= 0) return;
  91.  
  92.                 if (DeviceInserted != null)
  93.                     DeviceInserted(this, new DeviceChangedEventArgs {DeviceName = deviceName});
  94.  
  95.             }
  96.         }
  97.  
  98.         private void removeWatcher_EventArrived(object sender, EventArrivedEventArgs e)
  99.         {
  100.             var mbo = (ManagementBaseObject) e.NewEvent.Properties["TargetInstance"].Value;
  101.             string deviceName = GetDeviceName(mbo.Properties["DeviceID"].Value.ToString());
  102.             if (deviceName.Length <= 0) return;
  103.  
  104.             if (DeviceRemoved != null)
  105.                 DeviceRemoved(this, new DeviceChangedEventArgs {DeviceName = deviceName});
  106.         }
  107.  
  108.         #endregion
  109.  
  110.         #region Implemented Methods
  111.  
  112.         public void Dispose()
  113.         {
  114.             insertWatcher.Stop();
  115.             removeWatcher.Stop();
  116.  
  117.             insertWatcher.Dispose();
  118.             removeWatcher.Dispose();
  119.         }
  120.  
  121.         #endregion
  122.     }
  123.  
  124.     public class DeviceChangedEventArgs : EventArgs
  125.     {
  126.         public string DeviceName { get; set; }
  127.     }
  128.     // Usage
  129.     private DeviceDetector deviceDetector;
  130.    
  131.     private void Init()
  132.     {
  133.         deviceDetector= new DeviceDetector();
  134.         deviceDetector.DeviceInserted += deviceDetector_DeviceInserted;
  135.         deviceDetector.DeviceRemoved += deviceDetector_DeviceRemoved;
  136.     }
  137.  
  138.     private void Terminate()
  139.     {
  140.         deviceDetector.Dispose();
  141.     }  
  142.  
  143.     void deviceDetector_DeviceInserted(object sender, DeviceChangedEventArgs eventArgs)
  144.     {
  145.         Debug.WriteLine("Device Inserted: " + eventArgs.DeviceName);
  146.     }
  147.  
  148.     void deviceDetector_DeviceRemoved(object sender, DeviceChangedEventArgs eventArgs)
  149.     {
  150.         Debug.WriteLine("Device Removed: " + eventArgs.DeviceName);
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement