循环遍历硬盘上的所有目录

时间:2010-10-05 14:18:10

标签: vb.net visual-studio-2008

嘿,我有这段代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strFileSize As String = ""
    Dim di As New IO.DirectoryInfo("C:\")

    Try
        di.GetFiles("*.*", SearchOption.AllDirectories)
    Catch
    End Try

    Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
    Dim fi As IO.FileInfo

    For Each fi In aryFi
        strFileSize = (Math.Round(fi.Length / 1024)).ToString()
        Debug.Print("File Name: {0}", fi.Name)
        'Debug.Print("File Full Name: {0}", fi.FullName)
        'Debug.Print("File Size (KB): {0}", strFileSize)
        'Debug.Print("File Extension: {0}", fi.Extension)
        'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
    Next
End Sub

它工作得很好。但是,这就是说,我需要找到一种方法来遍历 ALL 目录在“c”驱动器而不仅仅是主文件夹。

有没有人有任何代码可以做到这一点?

谢谢!

解决

Imports System.IO
Imports System
Imports System.Collections.Generic

Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim vFolder As String = "c:\"
    WalkDirRecursive(vFolder)
End Sub

Private Sub WalkDirRecursive(ByVal vPath As String)
    Dim vDirInfo As New System.IO.DirectoryInfo(vPath)
    If Not vDirInfo.Exists Then Exit Sub

    'get all files' sizes in current path
    On Error Resume Next
    For Each vFile As String In System.IO.Directory.GetFiles(vDirInfo.FullName)

        'do something with this file
        Debug.Print(vFile)
    Next

    'do the same for all subfolders
    For Each vSubDir As String In System.IO.Directory.GetDirectories(vDirInfo.FullName)
        WalkDirRecursive(vSubDir)
    Next
End Sub

Private Sub RecurseDirectories(ByVal di As DirectoryInfo)
    Try
        For Each d In di.GetDirectories()
            ProcessData(d)
            RecurseDirectories(d)
        Next
    Catch
    End Try
End Sub

Private Sub ProcessData(ByVal di As IO.DirectoryInfo)
    Dim strFileSize As String = ""
    Dim fi As IO.FileInfo

    Try
        di.GetFiles("*.*", SearchOption.AllDirectories)
    Catch
    End Try

    Try
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")

        For Each fi In aryFi
            strFileSize = (Math.Round(fi.Length / 1024)).ToString()
            Debug.Print("File Name: {0}", fi.Name)
            'Debug.Print("File Full Name: {0}", fi.FullName)
            'Debug.Print("File Size (KB): {0}", strFileSize)
            'Debug.Print("File Extension: {0}", fi.Extension)
            'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
        Next
    Catch
    End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim di As New IO.DirectoryInfo("C:\")
    RecurseDirectories(di)
End Sub
End Class

大卫

3 个答案:

答案 0 :(得分:2)

你可以这样做:

di.GetFiles("*.*", SearchOption.AllDirectories)

答案 1 :(得分:2)

你需要递归循环所有文件夹,我找到了一篇文章Here!

答案 2 :(得分:1)

您可以在DirectoryInfo.GetFiles方法中将其指定为选项。

如果您要在Vista上部署,请确保正确处理访问权限。