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)); switch (DevDesc.dbcv_flags) { case VolumeChangeFlags.DBTF_MEDIA: OnDeviceChange(DevDesc, DeviceChangeEventType.MediaInserted); break; case VolumeChangeFlags.DBTF_NET: break; default: OnDeviceChange(DevDesc, DeviceChangeEventType.VolumeInserted); break; } } 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)); switch (DevDesc.dbcv_flags) { case VolumeChangeFlags.DBTF_MEDIA: OnDeviceChange(DevDesc, DeviceChangeEventType.MediaRemoved); break; case VolumeChangeFlags.DBTF_NET: break; default: OnDeviceChange(DevDesc, DeviceChangeEventType.VolumeRemoved); break; } } break; /*case DBT_DEVICETYPESPECIFIC : * break;*/ } } base.WndProc(ref m); }