Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Management;
- using Microsoft.Win32;
- public class DeviceDetector : IDisposable
- {
- #region Variables
- private ManagementEventWatcher insertWatcher;
- private ManagementEventWatcher removeWatcher;
- private readonly Guid GUID_DEVCLASS_MEDIA = new Guid("{4D36E96C-E325-11CE-BFC1-08002BE10318}");
- public delegate void DeviceInsertedHandler(object sender, DeviceChangedEventArgs eventArgs);
- public event DeviceInsertedHandler DeviceInserted;
- public delegate void DeviceRemovedHandler(object sender, DeviceChangedEventArgs eventArgs);
- public event DeviceRemovedHandler DeviceRemoved;
- #endregion
- #region Constructors
- public DeviceDetector()
- {
- EstablishWatchedEvents();
- }
- #endregion
- #region Private Methods
- private void EstablishWatchedEvents()
- {
- // setup the query to monitor insertion
- const string qryInsertion = "SELECT * FROM __InstanceCreationEvent " +
- "WITHIN 2 " +
- "WHERE TargetInstance ISA 'Win32_PnPEntity'";
- insertWatcher = new ManagementEventWatcher(qryInsertion);
- insertWatcher.EventArrived += insertWatcher_EventArrived;
- insertWatcher.Start();
- // setup the query to monitor removal
- const string qryRemoval = "SELECT TargetInstance " +
- "FROM __InstanceDeletionEvent " +
- "WITHIN 2 " +
- "WHERE TargetInstance ISA 'Win32_PnPEntity' ";
- removeWatcher = new ManagementEventWatcher(qryRemoval);
- removeWatcher.EventArrived += removeWatcher_EventArrived;
- removeWatcher.Start();
- }
- private string GetDeviceName(string deviceID)
- {
- string[] Parts = deviceID.Split(@"\".ToCharArray());
- if (Parts.Length >= 3)
- {
- string DevType = Parts[0];
- string DeviceInstanceId = Parts[1];
- string DeviceUniqueID = Parts[2];
- string RegPath = @"SYSTEM\CurrentControlSet\Enum\" + DevType + "\\" + DeviceInstanceId + "\\" +
- DeviceUniqueID;
- RegistryKey key = Registry.LocalMachine.OpenSubKey(RegPath);
- if (key != null)
- {
- object result = key.GetValue("FriendlyName");
- if (result != null)
- return result.ToString();
- }
- }
- return String.Empty;
- }
- #endregion
- #region Events
- private void insertWatcher_EventArrived(object sender, EventArrivedEventArgs e)
- {
- var mbo = (ManagementBaseObject) e.NewEvent["TargetInstance"];
- if (new Guid((string) mbo["ClassGuid"]) == GUID_DEVCLASS_MEDIA)
- {
- var deviceName = (string) mbo["Name"];
- if (deviceName.Length <= 0) return;
- if (DeviceInserted != null)
- DeviceInserted(this, new DeviceChangedEventArgs {DeviceName = deviceName});
- }
- }
- private void removeWatcher_EventArrived(object sender, EventArrivedEventArgs e)
- {
- var mbo = (ManagementBaseObject) e.NewEvent.Properties["TargetInstance"].Value;
- string deviceName = GetDeviceName(mbo.Properties["DeviceID"].Value.ToString());
- if (deviceName.Length <= 0) return;
- if (DeviceRemoved != null)
- DeviceRemoved(this, new DeviceChangedEventArgs {DeviceName = deviceName});
- }
- #endregion
- #region Implemented Methods
- public void Dispose()
- {
- insertWatcher.Stop();
- removeWatcher.Stop();
- insertWatcher.Dispose();
- removeWatcher.Dispose();
- }
- #endregion
- }
- public class DeviceChangedEventArgs : EventArgs
- {
- public string DeviceName { get; set; }
- }
- // Usage
- private DeviceDetector deviceDetector;
- private void Init()
- {
- deviceDetector= new DeviceDetector();
- deviceDetector.DeviceInserted += deviceDetector_DeviceInserted;
- deviceDetector.DeviceRemoved += deviceDetector_DeviceRemoved;
- }
- private void Terminate()
- {
- deviceDetector.Dispose();
- }
- void deviceDetector_DeviceInserted(object sender, DeviceChangedEventArgs eventArgs)
- {
- Debug.WriteLine("Device Inserted: " + eventArgs.DeviceName);
- }
- void deviceDetector_DeviceRemoved(object sender, DeviceChangedEventArgs eventArgs)
- {
- Debug.WriteLine("Device Removed: " + eventArgs.DeviceName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement