vb.net移除u盘 vb删除命令( 二 )


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
关于 VB.NET 2008中U盘的插拔一下代码将自动完成检测:
Imports System.IO
Public Class Form1
Public Const WM_DEVICECHANGE = H219
Public Const DBT_DEVICEARRIVAL = H8000
Public Const DBT_DEVICEREMOVECOMPLETE = H8004
Dim DriveLetter As String
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case DBT_DEVICEARRIVAL
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
DriveLetter = drive.Name.ToString()
Debug.WriteLine("U盘 :"DriveLetter"已插入.")
End If
Next
Case DBT_DEVICEREMOVECOMPLETE
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.ToString = DriveLetter Then Exit Sub
Next
Debug.WriteLine("U盘:"DriveLetter"已卸载!")
End Select
End If
MyBase.WndProc(m)
End Sub
End Class
vb.net编程接受U盘准备卸载用的windows消息 DBT_DEVICEQUERYREMOVE总是没反应,据说是因为默认注册的消息里在我的XP+VB2010环境下,测试U盘插入拔出,没有问题:
Private WM_DEVICECHANGE As Integer = H219
Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
DBT_CONFIGCHANGECANCELED = H19
DBT_CONFIGCHANGED = H18
DBT_CUSTOMEVENT = H8006
DBT_DEVICEARRIVAL = H8000
DBT_DEVICEQUERYREMOVE = H8001
DBT_DEVICEQUERYREMOVEFAILED = H8002
DBT_DEVICEREMOVECOMPLETE = H8004
DBT_DEVICEREMOVEPENDING = H8003
DBT_DEVICETYPESPECIFIC = H8005
DBT_DEVNODES_CHANGED = H7
DBT_QUERYCHANGECONFIG = H17
DBT_USERDEFINED = HFFFF
End Enum
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
MsgBox("USB device is just plugged in!")
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
MsgBox("USB device is just taken out")
End Select
End If
MyBase.WndProc(m)
End Sub
VB.NET检测插入U盘时执行某段代码 , 并能获取U盘盘符以下内容为网络提供,但我自己验证可行,供vb.net移除u盘你参考.
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盘插入
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盘已插入vb.net移除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

推荐阅读