方法很多,下面列举一个:
十多年的加查网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整加查建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“加查网站设计”,“加查网站推广”以来,每个客户项目都认真落实执行。
窗体上添加2个列表框,一个按钮:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDir As String = "C:\123"
'搜索并显示子文件夹
ListBox1.Items.Clear()
For Each MySubDir As String In System.IO.Directory.GetDirectories(MyDir)
ListBox1.Items.Add(MySubDir)
Next
'搜索并显示文件
ListBox2.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyDir)
ListBox2.Items.Add(MyFile)
Next
End Sub
End Class
给你一个提示吧!呵呵,剩下的要自己思考哦!
Path:是初始目录的路径
Private Sub WriteArray(ByVal Path As String)
'写入数组代码在这里,直接将Path写到数组就行了。
Dim dir As New IO.DirectoryInfo(Path)
For Each d As IO.DirectoryInfo In dir.GetDirectories
WriteArray(d.FullName) '递归
Next
End Sub
如果子目录较多那么递归会比较费时间,在调用递归前加入application.doevent就行了,这样在应对大递归时不会死机。
至于数组嘛使用 arraylist 最好了。
回答补充:
把递归去掉就行了啊!
解压密码:1299
假设路径为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 Sub
End Class
详见源代码!!
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文件(文件写入的编码要与读取的编码一致)
'把下面的目录改成你要遍历的目录
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