private void OnDeviceChange(DEV_BROADCAST_VOLUME DevDesc, DeviceChangeEventType EventType) { if (DeviceChange != null) { foreach (char ch in DevDesc.Drives) { DeviceChangeEventArgs a = new DeviceChangeEventArgs(ch, EventType); DeviceChange(this, a); } } }
protected override void WndProc(ref Message m) { if (m.Msg == WM_DEVICECHANGE) { DEV_BROADCAST_HDR head; switch (m.WParam.ToInt32()) { /*case DBT_DEVNODES_CHANGED : * break; * case DBT_CONFIGCHANGED : * break;*/ case DBT_DEVICEARRIVAL: head = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); if (head.dbch_devicetype == DeviceType.DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME DevDesc = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); if (DevDesc.dbcv_flags == VolumeChangeFlags.DBTF_MEDIA) { OnDeviceChange(DevDesc, DeviceChangeEventType.DeviceInserted); } } break; /*case DBT_DEVICEQUERYREMOVE : * break; * case DBT_DEVICEQUERYREMOVEFAILED : * break; * case DBT_DEVICEREMOVEPENDING : * break;*/ case DBT_DEVICEREMOVECOMPLETE: head = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); if (head.dbch_devicetype == DeviceType.DBT_DEVTYP_VOLUME) { DEV_BROADCAST_VOLUME DevDesc = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); if (DevDesc.dbcv_flags == VolumeChangeFlags.DBTF_MEDIA) { OnDeviceChange(DevDesc, DeviceChangeEventType.DeviceRemoved); } } break; /*case DBT_DEVICETYPESPECIFIC : * break;*/ } } base.WndProc(ref m); }