Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)'
- 'These are the required subclassing codes for detecting device based removal and arrival.
- '
- If M.Msg = WM_DEVICECHANGE Then
- Select M.WParam
- '
- 'Check if a device was added.
- Case DBT_DEVICEARRIVAL
- Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
- If DevType = DBT_DEVTYP_VOLUME Then
- Dim Vol As New DEV_BROADCAST_VOLUME
- Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
- If Vol.Dbcv_Flags = 0 Then
- For i As Integer = 0 To 20
- If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
- Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
- Dim d As IO.DriveInfo
- For Each d In usb
- If d.IsReady = True AndAlso d.DriveType = IO.DriveType.Removable Then
- unidades.Items.Add(d.Name & d.VolumeLabel).ToString()
- End If
- Next
- Exit For
- End If
- Next
- End If
- End If
- Case DBT_DEVICEREMOVECOMPLETE
- Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
- If DevType = DBT_DEVTYP_VOLUME Then
- Dim Vol As New DEV_BROADCAST_VOLUME
- Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
- If Vol.Dbcv_Flags = 0 Then
- For i As Integer = 0 To 20
- If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
- Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
- Dim d As IO.DriveInfo
- For Each d In usb
- If d.IsReady = False AndAlso d.DriveType = IO.DriveType.Removable Then
- unidades.Items.Remove(d.Name & d.VolumeLabel & usb.ToString)
- End If
- Next
- Exit For
- End If
- Next
- End If
- End If
- End Select
- End If
- MyBase.WndProc(M)
- private void Form1_Load(object sender, EventArgs e)
- {
- refreshUsb();
- }
- const int WM_DEVICECHANGE = 537;
- const int DBT_DEVICEARRIVAL = 0x8000;
- const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
- protected override void WndProc(ref Message m)
- {
- if (m.Msg == WM_DEVICECHANGE&&((int)m.WParam==DBT_DEVICEARRIVAL ||(int)m.WParam==DBT_DEVICEREMOVECOMPLETE ))
- refreshUsb();
- base.WndProc(ref m);
- }
- private void refreshUsb()
- {
- var usbs = System.IO.DriveInfo.GetDrives().Where(d => d.DriveType == System.IO.DriveType.Removable).ToArray();
- comboBox1.Items.Clear();
- comboBox1.Items.AddRange(usbs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement