以下內容為網絡提供,但我自己驗證可行,供你參考.
成都創(chuàng)新互聯是一家集網站建設,仁布企業(yè)網站建設,仁布品牌網站建設,網站定制,仁布網站建設報價,網絡營銷,網絡優(yōu)化,仁布網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
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盤已插入!盤符為:" + 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
ListBox1.Items.Add("請您現在插入U盤至USB接口!")
End Sub
Private Sub BtnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click
If ComboBox1.Text = "" Then
MsgBox("請選擇U盤盤符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Writer As StreamWriter = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Writer = New StreamWriter(fileName)
Writer.WriteLine(InputBox("老四,請輸入要保存的字符串", "輸入信息", "Input then Test String! hehe!"))
MsgBox("Write to " + fileName + " Success!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Write 失敗")
Finally
If Writer IsNot Nothing Then Writer.Close()
End Try
End If
End Sub
Private Sub BtnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRead.Click
If ComboBox1.Text = "" Then
MsgBox("請選擇U盤盤符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Reader As StreamReader = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Reader = New StreamReader(fileName)
MsgBox("Read from " + fileName + vbCrLf + Reader.ReadToEnd, MsgBoxStyle.Information, "Info")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Read 失敗")
Finally
If Reader IsNot Nothing Then Reader.Close()
End Try
End If
End Sub
End Class
依次讀出ds中每個項的VolumeLabel屬性就可以了Dim ds() As System.IO.DriveInfods = System.IO.DriveInfo.GetDrives();
預先準備三個圖標文件,用于樹型控件中顯示磁盤符號和文件夾的圖像之用。
1、窗體上添加控件如下:
組合框控件 ComboBox1,樹型控件 TreeView1,列表框控件 ListBox1,圖像列表控件 ImageList1。
選中TreeView1,設置其ImageList屬性為ImageList1。
2、設置屬性
選中圖像列表控件 ImageList1,在屬性窗口里,選中屬性Images,單擊三個小點按鈕,出現圖像集合編輯器窗口,單擊[添加按鈕],一一把準備好的圖標文件進行添加,注意先后次序,如果不符合要求可以通過上下移動按鈕重新改變次序。完成后單擊[確定]。
運行圖如下:
完整代碼如下:
Imports?System.IO
Public?Class?Form1
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
'添加系統(tǒng)所有磁盤目錄符號
For?Each?MyDrive?As?String?In?Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'顯示第一個磁盤符號
ComboBox1.Text?=?ComboBox1.Items(0)
End?Sub
'遞歸過程添加目錄樹
Public?Sub?AddDirectory(ByVal?strFatherPath?As?String,?ByVal?strPath?As?String,?ByVal?nodeFather?As?TreeNode)
Dim?i?As?Integer
Dim?Mynode?As?New?TreeNode
'先添加本目錄
Mynode.Text?=?Strings.Replace(strPath,?strFatherPath??"\",?"",?,?1)
'為節(jié)點指定未被選中時顯示的圖標
Mynode.ImageIndex?=?1
'為節(jié)點指定被選中時顯示的圖標
Mynode.SelectedImageIndex?=?2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim?str()?As?String?=?Directory.GetDirectories(strPath)
'遞歸遍歷該目錄的子文件夾
For?i?=?0?To?str.GetUpperBound(0)
AddDirectory(strPath,?str(i),?Mynode)
Next
Catch?ex?As?Exception
Debug.WriteLine(ex.Message)
End?Try
Mynode?=?Nothing
End?Sub
'根據給出的盤符添加目錄樹
Private?Sub?AddRootDirectory(ByVal?DiscSymbol?As?String)
Dim?Nynode?As?New?TreeNode
'先把磁盤盤符添加到樹中
TreeView1.Nodes.Clear()
Nynode.ImageIndex?=?0
Nynode.Text?=?DiscSymbol
Nynode.SelectedImageIndex?=?-1
TreeView1.Nodes.Add(Nynode)
Dim?i?As?Integer
'獲取磁盤根目錄下的文件夾
Dim?str()?As?String?=?Directory.GetDirectories(DiscSymbol??"\")
For?i?=?0?To?str.GetUpperBound(0)
'調用遞歸過程遍歷該文件夾里的所有子文件夾,并添加到樹型控件
AddDirectory(DiscSymbol,?str(i),?Nynode)
Next
Nynode?=?Nothing
End?Sub
Private?Sub?ComboBox1_SelectedIndexChanged(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?ComboBox1.SelectedIndexChanged
'根據磁盤符號的變更,顯示根目錄里的文件
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根據磁盤符號的變更,重新顯示目錄樹
Dim?DiscSymbol?As?String
DiscSymbol?=?Microsoft.VisualBasic.Left(ComboBox1.Text,?Len(ComboBox1.Text)?-?1)
Call?AddRootDirectory(DiscSymbol)
End?Sub
'遞歸過程根據子目錄尋找上級目錄名--從而構成完整的目錄路徑
Private?Sub?AllPath(ByVal?ThisNode?As?TreeNode,?ByRef?MyPathName?As?String)
If?ThisNode.Level??1?Then
'該節(jié)點層數大于1,其父節(jié)點不是磁盤根目錄
MyPathName?=?ThisNode.Parent.Text??"\"??MyPathName
Dim?MyNode?As?TreeNode?=?ThisNode.Parent
Call?AllPath(MyNode,?MyPathName)
Else
'該節(jié)點層數等于1,其父節(jié)點就是磁盤根目錄
MyPathName?=?ComboBox1.Text??MyPathName
End?If
End?Sub
Private?Sub?TreeView1_AfterSelect(ByVal?sender?As?System.Object,?ByVal?e?As?System.Windows.Forms.TreeViewEventArgs)?Handles?TreeView1.AfterSelect
'為了搜索選中的節(jié)點對應目錄的文件,需要組成全路徑
Dim?MyAllPathName?As?String?=?TreeView1.SelectedNode.Text
Dim?MyNode?As?TreeNode?=?TreeView1.SelectedNode
If?TreeView1.SelectedNode.Level?=?0?Then
'如果選中的是根節(jié)點
MyAllPathName?=?ComboBox1.Text
Else
'如果選中的是非根節(jié)點,調用遞歸過程組成全路徑
Call?AllPath(MyNode,?MyAllPathName)
MyAllPathName?=?MyAllPathName??"\"
End?If
'根據路徑,搜索文件名并顯示
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End?Sub
End?Class
Sub RenameVolume()
'利用Shell函數在命令提示符下執(zhí)行Diskpart命令的腳本,并存入日志文件
'達到替換邏輯盤符的目的
'在WindowsXp系統(tǒng)下測試成功
'獲取臨時文件路徑
Temp = Environ("TEMP") "\"
'獲取命令提示符路徑
Cmd = Environ("ComSpec")
'設置腳本文件和日志文件路徑
ScriptFile = Temp "DiskpartScript.txt"
ScriptLog = Temp "DiskpartScript.log"
'回車符和換行符
vbLfCr = vbLf vbCr
'腳本文件內容
'選擇卷e
'移除盤符e
'分配盤符m
'退出Diskpart
ScriptTxt = "select volume e" vbLf vbCr _
"Remove letter = e" vbLf vbCr _
"assign letter = m" vbLf vbCr _
"exit" vbLf vbCr
'打開腳本文件并讀取內容
Open ScriptFile For Output As #1
Print #1, ScriptTxt
Close #1
'打開錯誤處理
On Error Resume Next
'刪除原有日志文件
Kill ScriptLog
'關閉錯誤處理
On Error GoTo 0
'執(zhí)行腳本
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函數以字節(jié)方式讀取,否則長度會超出
LogTxt = InputB(LOF(1), #1)
Close #1
'轉換以字節(jié)方式讀取的日志文件內容
LogTxt = StrConv(LogTxt, vbUnicode)
'如果日志中出現兩次成功,則認為執(zhí)行成功,否則認為失敗
If Len(LogTxt) - Len(Replace(LogTxt, "成功", "", 1)) = 4 Then
MsgBox "盤符替換成功!"
Else
MsgBox "盤符替換失??!"
End If
'刪除臨時文件
Kill ScriptFile
Kill ScriptLog
End Sub