Hope this helps, its code based on Neeraj Dubbey’s answer.
We run code in a continuous multithreaded loop that keeps checking for a change in connected USB devices. Because we keep track of previously connected devices we can compare it against a new list of devices. We can also determine if a device is removed using the same method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Usb_Test
{
class Program
{
private static List<USBDeviceInfo> previousDecices = new List<USBDeviceInfo>();
static void Main(string[] args)
{
Thread thread = new Thread(DeviceDetection);
thread.Start();
Console.Read();
}
static void DeviceDetection()
{
while (true)
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
if (previousDecices == null || !previousDecices.Any()) // So we don't detect already plugged in devices the first time.
previousDecices = devices;
var insertionDevices = devices.Where(d => !previousDecices.Any(d2 => d2.DeviceID == d.DeviceID));
if (insertionDevices != null && insertionDevices.Any())
{
foreach(var value in insertionDevices)
{
Console.WriteLine("Inserted: " + value.DeviceID); // Add your own event for the insertion of devices.
}
}
var removedDevices = previousDecices.Where(d => !devices.Any(d2 => d2.DeviceID == d.DeviceID));
if (removedDevices != null && removedDevices.Any())
{
foreach (var value in removedDevices)
{
Console.WriteLine("Removed: " + value.DeviceID); // Add your own event for the removal of devices.
}
}
previousDecices = devices;
collection.Dispose();
}
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}
}
However this is not a great solution, you're a lot better off listening for WM_DEVICECHANGE.
Windows will send WM_DEVICECHANGE message to all applications whenever
some hardware change occurs, including when a flash drive (or other
removable device) is inserted or removed. The WParam parameter of this
message contains code which specifies exactly what event occurred. For
our purpose only the following events are interesting:
DBT_DEVICEARRIVAL - sent after a device or piece of media has been
inserted. Your program will receive this message when the device is
ready for use, at about the time when Explorer displays the dialog
which lets you choose what to do with the inserted media.
DBT_DEVICEQUERYREMOVE - sent when the system requests permission to
remove a device or piece of media. Any application can deny this
request and cancel the removal. This is the important event if you
need to perform some action on the flash drive before it is removed,
e.g. encrypt some files on it. Your program can deny this request
which will cause Windows to display the well-known message saying that
the device cannot be removed now.
DBT_DEVICEREMOVECOMPLETE - sent after a device has been removed. When
your program receives this event, the device is no longer available —
at the time when Windows display its "device has been removed" bubble
to the user.
Here are some links that each detail an answer on how to do this in C#:
http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program
http://www.codeproject.com/Articles/60579/A-USB-Library-to-Detect-USB-Devices
Both solution should work, and you can always adapt them to fit your needs.