using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Management; namespace WindowsFormsApplication2 { public partial class Form1 : Form { const string strCardReader = "XYZ"; public const int WM_DEVICECHANGE = 0x0219; public Form1() { InitializeComponent(); } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_DEVICECHANGE: OnDeviceChange(); break; } base.WndProc(ref m); // base.WndProc(ref m); } void OnDeviceChange() { List<string> list = new List<string>(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice"); foreach (ManagementObject mo in searcher.Get()) { string dependent = mo["Dependent"].ToString(); string deviceId = dependent.Split('=')[1]; deviceId = deviceId.Replace('\"', '\''); ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity Where DeviceID = " + deviceId); foreach (ManagementObject mo2 in searcher2.Get()) { string Description = mo2["Description"].ToString(); list.Add(Description); } } // remove duplicates, sort alphabetically and convert to array //string[] usbDevices = list.Distinct().OrderBy(s => s).ToArray(); if (!list.Contains(strCardReader)) label1.Text = "Not Available"; else label1.Text = ""; } } }
InstanceCreationEvent
InstanceDeletionEvent
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)