解壓密碼:1299
創(chuàng)新互聯(lián)公司是一家專業(yè)提供綠園企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站建設(shè)、html5、小程序制作等業(yè)務(wù)。10年已為綠園眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
假設(shè)路徑為C:\abc
代碼:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Files As ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles("C:\abc") Dim FileSize As Long, FileName As String = Nothing Dim oFileSize As Long For Each File As String In Files oFileSize = My.Computer.FileSystem.GetFileInfo(File).Length If oFileSize FileSize Then FileSize = oFileSize FileName = File End If Next Dim ListN As ListViewItem = New ListViewItem(My.Computer.FileSystem.GetName(FileName)) ListN.SubItems.Add(oFileSize " byte") ListN.SubItems.Add(My.Computer.FileSystem.GetFileInfo(FileName).Extension " 文件") ListN.SubItems.Add(FileName) ListView1.Items.Add(ListN) End SubEnd Class
詳見源代碼??!
給你一個(gè)提示吧!呵呵,剩下的要自己思考哦!
Path:是初始目錄的路徑
Private Sub WriteArray(ByVal Path As String)
'寫入數(shù)組代碼在這里,直接將Path寫到數(shù)組就行了。
Dim dir As New IO.DirectoryInfo(Path)
For Each d As IO.DirectoryInfo In dir.GetDirectories
WriteArray(d.FullName) '遞歸
Next
End Sub
如果子目錄較多那么遞歸會(huì)比較費(fèi)時(shí)間,在調(diào)用遞歸前加入application.doevent就行了,這樣在應(yīng)對(duì)大遞歸時(shí)不會(huì)死機(jī)。
至于數(shù)組嘛使用 arraylist 最好了。
回答補(bǔ)充:
把遞歸去掉就行了?。?/p>
'把下面的目錄改成你要遍歷的目錄
Dim MyDir = "C:\Windows"
Dim MyInfo = MyDir + "目錄下的所有文件包括:"
For Each MyFile In System.IO.Directory.GetFiles(MyDir)
MyInfo += Environment.NewLine + MyFile
Next
Me.RichTextBox1.Text =MyInfo
vb.net:
Dim path = System.IO.Directory.GetFiles("D:\Log\abc", "*.txt", SearchOption.AllDirectories):搜索該路徑下的所有txt類型的目錄及子目錄
path.AddRange(path)
Dim _RecordInfo As String
Dim _Reader As StreamReader
_Reader = New StreamReader(file, System.Text.Encoding.Default):'以Default編碼的形式讀取file文件(文件寫入的編碼要與讀取的編碼一致)
Private Sub GetChilren(ByVal Subject As String)
For Each i As String In My.Computer.FileSystem.GetDirectories(Subject)
GetChilren(i)'在這里遞歸
Next
For Each i As String In My.Computer.FileSystem.GetFiles(Subject)
'''txt,用全局變量或參數(shù)類數(shù)組、泛型、控件保存每個(gè)i
Next
End Sub
IO.Directory.GetDirectories("文件夾") '獲取文件夾中的所有子文件夾路徑。
IO.Directory.GetFiles("文件夾") '獲取文件夾下所有文件的路徑。
'子文件夾的子文件夾,用遞歸;