vb.net 实现u盘插入,拔出的识别

矫情吗;* 2022-09-28 14:29 289阅读 0赞

Imports System.IO

Public Class Form1
Public Const WM_DEVICECHANGE = &H219
Public Const DBT_DEVICEARRIVAL = &H8000
Public Const DBT_CONFIGCHANGECANCELED = &H19
Public Const DBT_CONFIGCHANGED = &H18
Public Const DBT_CUSTOMEVENT = &H8006
Public Const DBT_DEVICEQUERYREMOVE = &H8001
Public Const DBT_DEVICEQUERYREMOVEFAILED = &H8002
Public Const DBT_DEVICEREMOVECOMPLETE = &H8004
Public Const DBT_DEVICEREMOVEPENDING = &H8003
Public Const DBT_DEVICETYPESPECIFIC = &H8005
Public Const DBT_DEVNODES_CHANGED = &H7
Public Const DBT_QUERYCHANGECONFIG = &H17
Public Const DBT_USERDEFINED = &HFFFF

  1. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  2. If m.Msg = WM\_DEVICECHANGE Then
  3. Select Case m.WParam
  4. Case WM\_DEVICECHANGE
  5. Case DBT\_DEVICEARRIVAL 'U盘插入
  6. ComboBox1.Items.Clear()
  7. Dim s() As DriveInfo = DriveInfo.GetDrives
  8. For Each drive As DriveInfo In s
  9. If drive.DriveType = DriveType.Removable Then
  10. ListBox1.Items.Add("U盘已插入!盘符为:" + drive.Name.ToString())
  11. ComboBox1.Items.Add(drive.Name)
  12. End If
  13. Next
  14. BtnWrite.Enabled = True
  15. BtnRead.Enabled = True
  16. Case DBT\_CONFIGCHANGECANCELED
  17. Case DBT\_CONFIGCHANGED
  18. Case DBT\_CUSTOMEVENT
  19. Case DBT\_DEVICEQUERYREMOVE
  20. Case DBT\_DEVICEQUERYREMOVEFAILED
  21. Case DBT\_DEVICEREMOVECOMPLETE 'U盘卸载
  22. ListBox1.Items.Add("U盘卸载!")
  23. BtnWrite.Enabled = False
  24. BtnRead.Enabled = False
  25. Case DBT\_DEVICEREMOVEPENDING
  26. Case DBT\_DEVICETYPESPECIFIC
  27. Case DBT\_DEVNODES\_CHANGED
  28. Case DBT\_QUERYCHANGECONFIG
  29. Case DBT\_USERDEFINED
  30. End Select
  31. End If
  32. MyBase.WndProc(m)
  33. End Sub
  34. Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35. ListBox1.Items.Add("请您现在插入U盘至USB接口!")
  36. End Sub
  37. Private Sub BtnWrite\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click
  38. If ComboBox1.Text = "" Then
  39. MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
  40. Else
  41. Dim Writer As StreamWriter = Nothing
  42. Try
  43. Dim fileName As String = ComboBox1.Text + "Test.txt"
  44. Writer = New StreamWriter(fileName)
  45. Writer.WriteLine(InputBox("老四,请输入要保存的字符串", "输入信息", "Input then Test String! hehe!"))
  46. MsgBox("Write to " + fileName + " Success!")
  47. Catch ex As Exception
  48. MsgBox(ex.Message, MsgBoxStyle.Critical, "Write 失败")
  49. Finally
  50. If Writer IsNot Nothing Then Writer.Close()
  51. End Try
  52. End If
  53. End Sub
  54. Private Sub BtnRead\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRead.Click
  55. If ComboBox1.Text = "" Then
  56. MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
  57. Else
  58. Dim Reader As StreamReader = Nothing
  59. Try
  60. Dim fileName As String = ComboBox1.Text + "Test.txt"
  61. Reader = New StreamReader(fileName)
  62. MsgBox("Read from " + fileName + vbCrLf + Reader.ReadToEnd, MsgBoxStyle.Information, "Info")
  63. Catch ex As Exception
  64. MsgBox(ex.Message, MsgBoxStyle.Critical, "Read 失败")
  65. Finally
  66. If Reader IsNot Nothing Then Reader.Close()
  67. End Try
  68. End If
  69. End Sub

End Class

发表评论

表情:
评论列表 (有 0 条评论,289人围观)

还没有评论,来说两句吧...

相关阅读

    相关 U无法识别读取

    > U盘插进电脑里有提示音,但是打开我的电脑却没有U盘,点击桌面右下角的小三角,里面也有U盘图标,该怎么让它显示读取出来呢? ![Center][] 第1步:在桌面此电脑(