永发信息网

vb.net 如何判断u盘是否接入

答案:1  悬赏:10  手机版
解决时间 2021-03-17 16:00
  • 提问者网友:不要迷恋哥
  • 2021-03-16 19:33
vb.net 如何判断u盘是否接入
最佳答案
  • 五星知识达人网友:拾荒鲤
  • 2021-03-16 20:09
窗体上添加两个标签:Label1和Label2
下面代码经过VB 2005测试通过。

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

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盘插入
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
Label2.Text = "U盘已经插入!盘符为:" + drive.Name.ToString()
End If
Next
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盘卸载
Label2.Text = "U盘已经拔出!"
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
Label1.Text = "请操作U盘!"
Label2.Text = ""
End Sub
End Class
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯