strResult += 磁盤類型: System.Convert.ToInt16(disk(DriveType).ToString())End IfMsgBox(strResult)NextEnd Sub總結(jié):在VB.NET中,用API函數(shù)可以獲取硬盤信息。原來熟悉API函數(shù)VB6程序員,可以對(duì)API函數(shù)聲明進(jìn)行適當(dāng)?shù)母暮?,進(jìn)行調(diào)用。利用FSO(文件系統(tǒng)對(duì)象)的Scrrun.DLL,也可以獲得磁盤信息。在.net Framwork中,利用WMI可以獲取更多的關(guān)于機(jī)器硬件的詳細(xì)信息(參考System.Management命名空間)。
創(chuàng)新互聯(lián)專注于梅河口網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供梅河口營(yíng)銷型網(wǎng)站建設(shè),梅河口網(wǎng)站制作、梅河口網(wǎng)頁(yè)設(shè)計(jì)、梅河口網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造梅河口網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供梅河口網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
依次讀出ds中每個(gè)項(xiàng)的VolumeLabel屬性就可以了Dim ds() As System.IO.DriveInfods = System.IO.DriveInfo.GetDrives();
Sub RenameVolume()
'利用Shell函數(shù)在命令提示符下執(zhí)行Diskpart命令的腳本,并存入日志文件
'達(dá)到替換邏輯盤符的目的
'在WindowsXp系統(tǒng)下測(cè)試成功
'獲取臨時(shí)文件路徑
Temp = Environ("TEMP") "\"
'獲取命令提示符路徑
Cmd = Environ("ComSpec")
'設(shè)置腳本文件和日志文件路徑
ScriptFile = Temp "DiskpartScript.txt"
ScriptLog = Temp "DiskpartScript.log"
'回車符和換行符
vbLfCr = vbLf vbCr
'腳本文件內(nèi)容
'選擇卷e
'移除盤符e
'分配盤符m
'退出Diskpart
ScriptTxt = "select volume e" vbLf vbCr _
"Remove letter = e" vbLf vbCr _
"assign letter = m" vbLf vbCr _
"exit" vbLf vbCr
'打開腳本文件并讀取內(nèi)容
Open ScriptFile For Output As #1
Print #1, ScriptTxt
Close #1
'打開錯(cuò)誤處理
On Error Resume Next
'刪除原有日志文件
Kill ScriptLog
'關(guān)閉錯(cuò)誤處理
On Error GoTo 0
'執(zhí)行腳本
ShellTxt = Cmd " /c ""diskpart.exe /S " ScriptFile " " ScriptLog """"
Shell ShellTxt
'打開錯(cuò)誤處理
On Error Resume Next
Do
Err.Number = 0
'打開日志文件并讀取內(nèi)容,因日志文件存取速度慢,必須以鎖定方式打開,否則會(huì)出錯(cuò)
Open ScriptLog For Input Lock Read Write As #1
DoEvents
Loop Until Err.Number = 0
'關(guān)閉錯(cuò)誤處理
On Error GoTo 0
'日志文件中含有中文字符,必須以InputB函數(shù)以字節(jié)方式讀取,否則長(zhǎng)度會(huì)超出
LogTxt = InputB(LOF(1), #1)
Close #1
'轉(zhuǎn)換以字節(jié)方式讀取的日志文件內(nèi)容
LogTxt = StrConv(LogTxt, vbUnicode)
'如果日志中出現(xiàn)兩次成功,則認(rèn)為執(zhí)行成功,否則認(rèn)為失敗
If Len(LogTxt) - Len(Replace(LogTxt, "成功", "", 1)) = 4 Then
MsgBox "盤符替換成功!"
Else
MsgBox "盤符替換失??!"
End If
'刪除臨時(shí)文件
Kill ScriptFile
Kill ScriptLog
End Sub
首先使用 System.IO.DriveInfo.GetDrives()獲取System.IO.DriveInfo,存入ds()
然后遍歷ds,獲取各個(gè)信息部分。
Dim ds() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
For i As Integer = 0 To ds.Length - 1
TextBox1.Text = TextBox1.Text + ds(i).DriveType.ToString + " " '驅(qū)動(dòng)器類型
TextBox1.Text = TextBox1.Text + ds(i).Name + " " '盤符(驅(qū)動(dòng)器名)
TextBox1.Text = TextBox1.Text + ds(i).IsReady.ToString + " " '是否就緒
If ds(i).IsReady = True Then
TextBox1.Text = TextBox1.Text + ds(i).VolumeLabel + " " '卷標(biāo)
TextBox1.Text = TextBox1.Text + ds(i).TotalSize.ToString + " " '驅(qū)動(dòng)器容量
TextBox1.Text = TextBox1.Text + ds(i).TotalFreeSpace.ToString '驅(qū)動(dòng)器可用容量
End If
TextBox1.Text = TextBox1.Text + vbNewLine
Next
以下內(nèi)容為網(wǎng)絡(luò)提供,但我自己驗(yàn)證可行,供你參考.
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("請(qǐng)您現(xiàn)在插入U(xiǎn)盤至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("請(qǐng)選擇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("老四,請(qǐng)輸入要保存的字符串", "輸入信息", "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("請(qǐng)選擇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
預(yù)先準(zhǔn)備三個(gè)圖標(biāo)文件,用于樹型控件中顯示磁盤符號(hào)和文件夾的圖像之用。
1、窗體上添加控件如下:
組合框控件 ComboBox1,樹型控件 TreeView1,列表框控件 ListBox1,圖像列表控件 ImageList1。
選中TreeView1,設(shè)置其ImageList屬性為ImageList1。
2、設(shè)置屬性
選中圖像列表控件 ImageList1,在屬性窗口里,選中屬性Images,單擊三個(gè)小點(diǎn)按鈕,出現(xiàn)圖像集合編輯器窗口,單擊[添加按鈕],一一把準(zhǔn)備好的圖標(biāo)文件進(jìn)行添加,注意先后次序,如果不符合要求可以通過上下移動(dòng)按鈕重新改變次序。完成后單擊[確定]。
運(yùn)行圖如下:
完整代碼如下:
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)所有磁盤目錄符號(hào)
For?Each?MyDrive?As?String?In?Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'顯示第一個(gè)磁盤符號(hào)
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é)點(diǎn)指定未被選中時(shí)顯示的圖標(biāo)
Mynode.ImageIndex?=?1
'為節(jié)點(diǎn)指定被選中時(shí)顯示的圖標(biāo)
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
'根據(jù)給出的盤符添加目錄樹
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)
'調(diào)用遞歸過程遍歷該文件夾里的所有子文件夾,并添加到樹型控件
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
'根據(jù)磁盤符號(hào)的變更,顯示根目錄里的文件
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根據(jù)磁盤符號(hào)的變更,重新顯示目錄樹
Dim?DiscSymbol?As?String
DiscSymbol?=?Microsoft.VisualBasic.Left(ComboBox1.Text,?Len(ComboBox1.Text)?-?1)
Call?AddRootDirectory(DiscSymbol)
End?Sub
'遞歸過程根據(jù)子目錄尋找上級(jí)目錄名--從而構(gòu)成完整的目錄路徑
Private?Sub?AllPath(ByVal?ThisNode?As?TreeNode,?ByRef?MyPathName?As?String)
If?ThisNode.Level??1?Then
'該節(jié)點(diǎn)層數(shù)大于1,其父節(jié)點(diǎn)不是磁盤根目錄
MyPathName?=?ThisNode.Parent.Text??"\"??MyPathName
Dim?MyNode?As?TreeNode?=?ThisNode.Parent
Call?AllPath(MyNode,?MyPathName)
Else
'該節(jié)點(diǎn)層數(shù)等于1,其父節(jié)點(diǎn)就是磁盤根目錄
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é)點(diǎn)對(duì)應(yīng)目錄的文件,需要組成全路徑
Dim?MyAllPathName?As?String?=?TreeView1.SelectedNode.Text
Dim?MyNode?As?TreeNode?=?TreeView1.SelectedNode
If?TreeView1.SelectedNode.Level?=?0?Then
'如果選中的是根節(jié)點(diǎn)
MyAllPathName?=?ComboBox1.Text
Else
'如果選中的是非根節(jié)點(diǎn),調(diào)用遞歸過程組成全路徑
Call?AllPath(MyNode,?MyAllPathName)
MyAllPathName?=?MyAllPathName??"\"
End?If
'根據(jù)路徑,搜索文件名并顯示
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End?Sub
End?Class