Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)'
  2. 'These are the required subclassing codes for detecting device based removal and arrival.
  3. '
  4.  
  5. If M.Msg = WM_DEVICECHANGE Then
  6. Select M.WParam
  7. '
  8. 'Check if a device was added.
  9. Case DBT_DEVICEARRIVAL
  10.  
  11. Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
  12.  
  13. If DevType = DBT_DEVTYP_VOLUME Then
  14.  
  15. Dim Vol As New DEV_BROADCAST_VOLUME
  16.  
  17. Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
  18.  
  19. If Vol.Dbcv_Flags = 0 Then
  20.  
  21. For i As Integer = 0 To 20
  22.  
  23. If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
  24. Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
  25. Dim d As IO.DriveInfo
  26. For Each d In usb
  27. If d.IsReady = True AndAlso d.DriveType = IO.DriveType.Removable Then
  28. unidades.Items.Add(d.Name & d.VolumeLabel).ToString()
  29. End If
  30. Next
  31. Exit For
  32. End If
  33. Next
  34. End If
  35. End If
  36.  
  37. Case DBT_DEVICEREMOVECOMPLETE
  38.  
  39. Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
  40.  
  41. If DevType = DBT_DEVTYP_VOLUME Then
  42.  
  43. Dim Vol As New DEV_BROADCAST_VOLUME
  44.  
  45. Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
  46.  
  47. If Vol.Dbcv_Flags = 0 Then
  48.  
  49. For i As Integer = 0 To 20
  50.  
  51. If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
  52.  
  53. Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
  54. Dim d As IO.DriveInfo
  55. For Each d In usb
  56. If d.IsReady = False AndAlso d.DriveType = IO.DriveType.Removable Then
  57. unidades.Items.Remove(d.Name & d.VolumeLabel & usb.ToString)
  58. End If
  59. Next
  60. Exit For
  61. End If
  62. Next
  63. End If
  64. End If
  65. End Select
  66. End If
  67.  
  68. MyBase.WndProc(M)
  69.  
  70. private void Form1_Load(object sender, EventArgs e)
  71. {
  72. refreshUsb();
  73. }
  74. const int WM_DEVICECHANGE = 537;
  75. const int DBT_DEVICEARRIVAL = 0x8000;
  76. const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
  77. protected override void WndProc(ref Message m)
  78. {
  79. if (m.Msg == WM_DEVICECHANGE&&((int)m.WParam==DBT_DEVICEARRIVAL ||(int)m.WParam==DBT_DEVICEREMOVECOMPLETE ))
  80. refreshUsb();
  81.  
  82. base.WndProc(ref m);
  83. }
  84.  
  85. private void refreshUsb()
  86. {
  87. var usbs = System.IO.DriveInfo.GetDrives().Where(d => d.DriveType == System.IO.DriveType.Removable).ToArray();
  88. comboBox1.Items.Clear();
  89. comboBox1.Items.AddRange(usbs);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement