当前位置:编程学习 > C#/ASP.NET >>

请问VB.net怎样读取全部子目录和孙目录

某一个文件夹里有多个子目录或孙目录,如果只给一个根目录,请问Vb.net怎样获取该文件夹里面的子目录和孙目录的路径  名称之类的 --------------------编程问答-------------------- http://www.opent.cn/a/0912/2148.shtml --------------------编程问答-------------------- 用递归调用System.Directory.GetDirectories和System.Directory.GetFiles就行了
--------------------编程问答-------------------- Imports System.IO

Dim strPath As String = 指定目录
Dim strDir As String
Dim strFile As String 
For Each strDir In Directory.GetDirectories(strPath) '获取指定目录中子目录的名称
Next

For Each strFile In Directory.GetFiles(strDir) '返回指定目录中的文件的名称
Next
--------------------编程问答-------------------- Directory.GetDirectories, Directory.GetFiles 可以指定是否要递归的。 --------------------编程问答-------------------- Public Sub FindFile(dir1 As String)
Dim Dir2 As New DirectoryInfo(dir1)
Try
For Each d As DirectoryInfo In Dir2.GetDirectories()
FindFile(Dir2 + d.ToString() + "\")
Next
For Each f As FileInfo In Dir2.GetFiles("*.*")
Next
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
--------------------编程问答--------------------

   For Each tempdir In dir.GetDirectories()
            Dim name As String = tempdir.FullName
             msgbox(name)
        Next

至于孙目录就要用到递归了 --------------------编程问答-------------------- 修改一下人生如梦的代码,可遍历子目录下的所有文件。如下:
Public Sub FindFile(ByVal dir1 As String)
        Dim Dir2 As New DirectoryInfo(dir1)

        Try
            For Each d As DirectoryInfo In Dir2.GetDirectories()
                FindFile(Dir2.ToString & "\" & d.ToString)

            Next
            For Each f As FileInfo In Dir2.GetFiles("*.*")
                MessageBox.Show(f.ToString)
            Next
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try
    End Sub
--------------------编程问答-------------------- vb.net本身就支持遍历下级所有目录的吧

        For Each Dir As IO.DirectoryInfo In New IO.DirectoryInfo(path).GetDirectories("*", IO.SearchOption.AllDirectories)
            MsgBox(Dir.FullName)      '取得目录完整路径
            MsgBox(Dir.Name)          '取得目录名
        Next
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,