关于U盘识别的问题
1.以下代码已经实现U盘识别。但是存在一个问题,当页面加载的时候并不能第一时间识别是否插入U盘(打开程序时,即使U盘已经插入也无法识别)。只能在当有U盘或者USB口有插或者拔得事件发生时,才会判断U盘是否插入。
总结:个人觉得可以在load中调用WndProc此方法,但是关于System.Windows.Forms.Message这个参数,如何获得暂时没有头绪。
代码如下:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE
Case DBT_DEVICEARRIVAL 'U盘插入
ComboBox1.Items.Clear()
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
ListBox1.Items.Add("U盘已插入!盘符为:" + drive.Name.ToString())
ComboBox1.Items.Add(drive.Name)
End If
Next
BtnWrite.Enabled = True
BtnRead.Enabled = True
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盘卸载
ListBox1.Items.Add("U盘卸载!")
BtnWrite.Enabled = False
BtnRead.Enabled = False
Case DBT_DEVICEREMOVEPENDING
Case DBT_DEVICETYPESPECIFIC
Case DBT_DEVNODES_CHANGED
Case DBT_QUERYCHANGECONFIG
Case DBT_USERDEFINED
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'加载窗体
'WndProc() System.Windows.Forms.Message这个参数如何获得?
End Sub