Public Partial classForm1:form {#regionU-Disk Properties Public Const intWm_devicechange =0x219;//once the USB stick is plugged in, the bottom of the OS is automatically detected and a "Hardware Device status change" message is sent to the application Public Const intDbt_devicearrival =0x8000;//is used to indicate that a USB flash drive is available. A device or media has been inserted into a piece and is now available. Public Const intdbt_configchangecanceled =0x0019;//the requirement to change the current configuration (or undock dock) has been canceled. Public Const intDbt_configchanged =0x0018;//The current configuration has changed due to dock or unpin. Public Const intDbt_customevent =0x8006;//a custom event occurs. Windows NT 4.0 and Windows 95: This value is not supported. Public Const intDbt_devicequeryremove =0x8001;//approval requires removal of a device or media work. No application can deny this request and cancel the deletion. Public Const intdbt_devicequeryremovefailed =0x8002;//request to delete a device or media slice has been canceled. Public Const intDbt_deviceremovecomplete =0x8004;//a device or media slice has been deleted. Public Const intDbt_deviceremovepending =0x8003;//a piece of equipment or media is about to be deleted. Not to be denied. Public Const intDbt_devicetypespecific =0x8005;//a device-specific event occurs. Public Const intDbt_devnodes_changed =0x0007;//a device has been added to or removed from the system. Public Const intDbt_querychangeconfig =0x0017;//The license is required to change the current configuration (dock or unpin). Public Const intdbt_userdefined =0xFFFF;//The meaning of this message is a user-defined #endregion PublicForm1 () {InitializeComponent (); } #regionDetect U-Diskprotected Override voidWndProc (refMessage m) { Try { if(M.msg = =wm_devicechange) { Switch(M.wparam.toint32 ()) { CaseWm_devicechange: Break; CaseDbt_devicearrival://USB InsertMessageBox.Show ("USB Insert"); Break; Casedbt_configchangecanceled: Break; Casedbt_configchanged: Break; Casedbt_customevent: Break; CaseDbt_devicequeryremove: Break; Casedbt_devicequeryremovefailed: Break; CaseDbt_deviceremovecomplete://USB Stick UninstallMessageBox.Show ("USB stick Pull out! "); Break; Casedbt_deviceremovepending: Break; Casedbt_devicetypespecific: Break; Casedbt_devnodes_changed: Break; CaseDbt_querychangeconfig: Break; Casedbt_userdefined: Break; default: Break; } } } Catch(Exception ex) {MessageBox.Show (ex). Message); } Base. WndProc (refm); } #endregion }
C # detects if the USB drive is plugged