I have located the following code and have been trying to change this so that I know if the USB device has been inserted or removed.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace WMIUSBConsolApplication
{
class Program
{
static void Main(string[] args)
{
AddInsetUSBHandler();
AddRemoveUSBHandler();
for (; ; ) ;
}
static ManagementEventWatcher w = null;
public static void AddRemoveUSBHandler()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceDeletionEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = @"TargetInstance ISA 'Win32_USBHub'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += new EventArrivedEventHandler(USBRemoved);
w.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (w != null)
w.Stop();
}
}
static void AddInsetUSBHandler()
{
WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = @"TargetInstance ISA 'Win32_USBHub'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += new EventArrivedEventHandler(USBAdded);
w.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (w != null)
w.Stop();
}
}
public static void USBAdded(object sender, EventArgs e)
{
Console.WriteLine("A USB device inserted");
}
public static void USBRemoved(object sender, EventArgs e)
{
Console.WriteLine("A USB device removed");
}
}
}

Manoj PrabhuPosted Dec 2, 2016, 6:04 AM
Private static List<string> GetConnectedUsbDrives() { List<string> usbDriveLetters = new List<string>(); var usbDrives = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"); foreach (ManagementObject drive in usbDrives.Get()) { var partitions = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"].ToString() + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"); foreach (var partition in partitions.Get()) { var logicalDrives = new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"].ToString() + "'} WHERE AssocClass = Win32_LogicalDiskToPartition"); foreach (var logicalDrive in logicalDrives.Get()) { usbDriveLetters.Add(logicalDrive["Caption"].ToString()); } } } return usbDriveLetters; }
richard paynePosted Oct 16, 2016, 3:49 PM
I have tried unsubscribe the event but does not work.
richard paynePosted Oct 16, 2016, 3:49 PM
Private void MainProg_FormClosing(object sender, FormClosingEventArgs e) { if (myUSBComm.wUSB != null) { myUSBComm.wUSB.EventArrived -= myUSBComm.USBRemoved; myUSBComm.wUSB.EventArrived -= myUSBComm.USBAdded; myUSBComm.wUSB.Stop(); myUSBComm.wUSB.Dispose(); } myGlobalBase.IsApplicationQuit = true; Thread.Sleep(50); }
richard paynePosted Oct 16, 2016, 3:48 PM
I have copied the above into application in VS2015 in C#, which worked very well. The only issue that when I close window while VS2015 is in debug mode, it generate Break report page in VS2015 reporting exception related to above.
Ansari TahirPosted May 17, 2014, 4:05 AM
nice...