用Io.Directory.GetFiles("D:\")函數(shù)獲得一個(gè)存放D盤(pán)目錄文件的字符串?dāng)?shù)組
創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、鹿寨網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、鹿寨網(wǎng)絡(luò)營(yíng)銷(xiāo)、鹿寨企業(yè)策劃、鹿寨品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供鹿寨建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):13518219792,官方網(wǎng)址:www.cdcxhl.com
代碼:
For Each item As String In IO.Directory.GetFiles("D:\")
ListBox1.Items.Add(item)
Next
運(yùn)行之后,ListBox1里就會(huì)有這些文件
首先使用 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)器類(lèi)型
TextBox1.Text = TextBox1.Text + ds(i).Name + " " '盤(pán)符(驅(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
總結(jié):在VB.NET中,用API函數(shù)可以獲取硬盤(pán)信息。原來(lái)熟悉API函數(shù)VB6程序員,可以對(duì)API函數(shù)聲明進(jìn)行適當(dāng)?shù)母暮螅M(jìn)行調(diào)用。利用FSO(文件系統(tǒng)對(duì)象)的Scrrun.DLL,也可以獲得磁盤(pán)信息。
預(yù)先準(zhǔn)備三個(gè)圖標(biāo)文件,用于樹(shù)型控件中顯示磁盤(pán)符號(hào)和文件夾的圖像之用。
1、窗體上添加控件如下:
組合框控件 ComboBox1,樹(shù)型控件 TreeView1,列表框控件 ListBox1,圖像列表控件 ImageList1。
選中TreeView1,設(shè)置其ImageList屬性為ImageList1。
2、設(shè)置屬性
選中圖像列表控件 ImageList1,在屬性窗口里,選中屬性Images,單擊三個(gè)小點(diǎn)按鈕,出現(xiàn)圖像集合編輯器窗口,單擊[添加按鈕],一一把準(zhǔn)備好的圖標(biāo)文件進(jìn)行添加,注意先后次序,如果不符合要求可以通過(guò)上下移動(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)所有磁盤(pán)目錄符號(hào)
For?Each?MyDrive?As?String?In?Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'顯示第一個(gè)磁盤(pán)符號(hào)
ComboBox1.Text?=?ComboBox1.Items(0)
End?Sub
'遞歸過(guò)程添加目錄樹(shù)
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ù)給出的盤(pán)符添加目錄樹(shù)
Private?Sub?AddRootDirectory(ByVal?DiscSymbol?As?String)
Dim?Nynode?As?New?TreeNode
'先把磁盤(pán)盤(pán)符添加到樹(shù)中
TreeView1.Nodes.Clear()
Nynode.ImageIndex?=?0
Nynode.Text?=?DiscSymbol
Nynode.SelectedImageIndex?=?-1
TreeView1.Nodes.Add(Nynode)
Dim?i?As?Integer
'獲取磁盤(pán)根目錄下的文件夾
Dim?str()?As?String?=?Directory.GetDirectories(DiscSymbol??"\")
For?i?=?0?To?str.GetUpperBound(0)
'調(diào)用遞歸過(guò)程遍歷該文件夾里的所有子文件夾,并添加到樹(shù)型控件
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ù)磁盤(pán)符號(hào)的變更,顯示根目錄里的文件
ListBox1.Items.Clear()
For?Each?MyFile?As?String?In?System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根據(jù)磁盤(pán)符號(hào)的變更,重新顯示目錄樹(shù)
Dim?DiscSymbol?As?String
DiscSymbol?=?Microsoft.VisualBasic.Left(ComboBox1.Text,?Len(ComboBox1.Text)?-?1)
Call?AddRootDirectory(DiscSymbol)
End?Sub
'遞歸過(guò)程根據(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)不是磁盤(pá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)就是磁盤(pá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)用遞歸過(guò)程組成全路徑
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
先引用 System.ManageMent
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim HDDDeviceQuery As New Management.SelectQuery("Win32_DiskDrive")
Dim HDDDeviceSearch As New Management.ManagementObjectSearcher(HDDDeviceQuery)
Dim HDDDeviceInfo As Management.ManagementObject
For Each HDDDeviceInfo In HDDDeviceSearch.Get()
ComboBox1.Items.Add("HDD Description: " HDDDeviceInfo("caption").ToString())
ComboBox1.Items.Add("HDD BytesPerSector: " HDDDeviceInfo("BytesPerSector").ToString())
'ComboBox1.Items.Add("HDD CompressionMethod: " HDDDeviceInfo("CompressionMethod").ToString())
ComboBox1.Items.Add("HDD Index: " HDDDeviceInfo("Index").ToString())
'ComboBox1.Items.Add("HDD InstallDate: " HDDDeviceInfo("InstallDate").ToString())
ComboBox1.Items.Add("HDD Manufacturer: " HDDDeviceInfo("Manufacturer").ToString())
ComboBox1.Items.Add("HDD Partitions: " HDDDeviceInfo("Partitions").ToString() Space(1) GetLastDev())
ComboBox1.Items.Add("HDD Size: " Int(Val(HDDDeviceInfo("Size").ToString()) / 2 ^ 30) " GBytes")
ComboBox1.Items.Add("HDD TotalCylinders: " HDDDeviceInfo("TotalCylinders").ToString())
ComboBox1.Items.Add("HDD TotalSectors: " HDDDeviceInfo("TotalSectors").ToString())
ComboBox1.Items.Add("HDD TracksPerCylinder: " HDDDeviceInfo("TracksPerCylinder").ToString())
ComboBox1.Items.Add("HDD TotalHeads: " HDDDeviceInfo("TotalHeads").ToString())
ComboBox1.Items.Add("HDD TotalTracks: " HDDDeviceInfo("TotalTracks").ToString())
ComboBox1.Items.Add("HDD SectorsPerTrack: " HDDDeviceInfo("SectorsPerTrack").ToString())
ComboBox1.Items.Add("HDD SCSILogicalUnit: " HDDDeviceInfo("SCSILogicalUnit").ToString())
Next
End Sub
Public Function GetLastDev() As String
GetLastDev = ""
Dim r As Short = My.Computer.FileSystem.Drives.Count
For i As Short = 0 To r - 1 Step 1
GetLastDev = GetLastDev My.Computer.FileSystem.Drives(i).Name ":"
Next
Return GetLastDev
End Function
End Class
系統(tǒng)路徑
System.Environment.SystemDirectory
然后自己再拼那個(gè)臨時(shí)路徑
為了確定路徑是否存在,別忘了進(jìn)行驗(yàn)證。