vb.net移除u盘 vb删除命令

vb(vb.net)能不能修改计算机中指定驱动器的盘符? 我想在U盘插入时通过序列号识别指定设备并Sub RenameVolume()
'利用Shell函数在命令提示符下执行Diskpart命令的脚本,并存入日志文件
'达到替换逻辑盘符的目的
'在WindowsXp系统下测试成功
'获取临时文件路径
Temp = Environ("TEMP")"\"
'获取命令提示符路径
Cmd = Environ("ComSpec")
'设置脚本文件和日志文件路径
ScriptFile = Temp"DiskpartScript.txt"
ScriptLog = Temp"DiskpartScript.log"
'回车符和换行符
vbLfCr = vbLfvbCr
'脚本文件内容
'选择卷e
'移除盘符e
'分配盘符m
'退出Diskpart
ScriptTxt = "select volume e"vbLfvbCr_
"Remove letter = e"vbLfvbCr_
"assign letter = m"vbLfvbCr_
"exit"vbLfvbCr
'打开脚本文件并读取内容
Open ScriptFile For Output As #1
Print #1, ScriptTxt
Close #1
'打开错误处理
On Error Resume Next
'删除原有日志文件
Kill ScriptLog
'关闭错误处理
On Error GoTo 0
【vb.net移除u盘 vb删除命令】'执行脚本
ShellTxt = Cmd" /c ""diskpart.exe /S "ScriptFile""ScriptLog""""
Shell ShellTxt
'打开错误处理
On Error Resume Next
Do
Err.Number = 0
'打开日志文件并读取内容,因日志文件存取速度慢,必须以锁定方式打开,否则会出错
Open ScriptLog For Input Lock Read Write As #1
DoEvents
Loop Until Err.Number = 0
'关闭错误处理
On Error GoTo 0
'日志文件中含有中文字符,必须以InputB函数以字节方式读取,否则长度会超出
LogTxt = InputB(LOF(1), #1)
Close #1
'转换以字节方式读取的日志文件内容
LogTxt = StrConv(LogTxt, vbUnicode)
'如果日志中出现两次成功,则认为执行成功,否则认为失败
If Len(LogTxt) - Len(Replace(LogTxt, "成功", "", 1)) = 4 Then
MsgBox "盘符替换成功!"
Else
MsgBox "盘符替换失败!"
End If
'删除临时文件
Kill ScriptFile
Kill ScriptLog
End Sub
vb.net 如何判断u盘是否接入窗体上添加两个标签: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

推荐阅读