How to detect the event when new USB is connected?
In this article I am going to discuss, How to trigger an event when a device is added or removed from the system? Let's start with an example and implement the USB detector.
Step 1- Create a new WPF project.
Step 2- Add new class "USBDetector" in solution.
Step 3 - Open "MainWindow.xaml" add a new button for start USB monitoring and add following code on click event of a button.
Start application and click on start USB monitoring button. Now insert a USB it will show a popup new USB is detected.
Download Source Code from link- Source Code
Step 1- Create a new WPF project.
Step 2- Add new class "USBDetector" in solution.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace WpfUSBDetection { /// <summary> /// To get USBBroadcastinterface /// </summary> [StructLayout(LayoutKind.Sequential)] public struct USBBroadcastinterface { /// <summary> /// The size /// </summary> internal int Size; /// <summary> /// The device type /// </summary> internal int USBType; /// <summary> /// The reserved /// </summary> internal int Reserved; /// <summary> /// The class unique identifier /// </summary> internal Guid ClassGuid; /// <summary> /// The name /// </summary> internal short Name; } /// <summary> /// To get DeviceDiscoveryManager /// </summary> public class USBDetector { /// <summary> /// The new usb device connected /// </summary> public const int NewUsbDeviceConnected = 0x8000; /// <summary> /// The usb device removed /// </summary> public const int UsbDeviceRemoved = 0x8004; /// <summary> /// The usb devicechange /// </summary> public const int UsbDevicechange = 0x0219; /// <summary> /// The DBT devtyp deviceinterface /// </summary> private const int DbtDevtypDeviceinterface = 5; /// <summary> /// The unique identifier devinterface usb device /// </summary> private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices /// <summary> /// The notification handle /// </summary> private static IntPtr notificationHandle; /// <summary> /// Registers a window to receive notifications when USB devices are plugged or unplugged. /// </summary> /// <param name="windowHandle">Handle to the window receiving notifications.</param> public static void RegisterUsbDeviceNotification(IntPtr windowHandle) { USBBroadcastinterface dbi = new USBBroadcastinterface { USBType = DbtDevtypDeviceinterface, Reserved = 0, ClassGuid = GuidDevinterfaceUSBDevice, Name = 0 }; dbi.Size = Marshal.SizeOf(dbi); IntPtr buffer = Marshal.AllocHGlobal(dbi.Size); Marshal.StructureToPtr(dbi, buffer, true); notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0); } /// <summary> /// Unregisters the window for USB device notifications /// </summary> public static void UnregisterUsbDeviceNotification() { UnregisterDeviceNotification(notificationHandle); } /// <summary> /// Registers the device notification. /// </summary> /// <param name="recipient">The recipient.</param> /// <param name="notificationFilter">The notification filter.</param> /// <param name="flags">The flags.</param> /// <returns>returns IntPtr</returns> [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)] private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags); /// <summary> /// Unregisters the device notification. /// </summary> /// <param name="handle">The handle.</param> /// <returns>returns bool</returns> [DllImport("user32.dll")] private static extern bool UnregisterDeviceNotification(IntPtr handle); } }
Step 3 - Open "MainWindow.xaml" add a new button for start USB monitoring and add following code on click event of a button.
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { HwndSource hwndSource = HwndSource.FromHwnd(Process.GetCurrentProcess().MainWindowHandle); if (hwndSource != null) { IntPtr windowHandle = hwndSource.Handle; hwndSource.AddHook(UsbNotificationHandler); USBDetector.RegisterUsbDeviceNotification(windowHandle); } USBButton.IsEnabled = false; } private IntPtr UsbNotificationHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { if (msg == USBDetector.UsbDevicechange) { switch ((int)wparam) { case USBDetector.UsbDeviceRemoved: MessageBox.Show("USB Removed"); break; case USBDetector.NewUsbDeviceConnected: MessageBox.Show("New USB Detected"); break; } } handled = false; return IntPtr.Zero; } }
Start application and click on start USB monitoring button. Now insert a USB it will show a popup new USB is detected.
Download Source Code from link- Source Code
0 comments :
Post a Comment