views:

39

answers:

3

Hey all i have this piece of code:

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

and it works just fine. However, saying that, i need to find a way to loop through ALL the directory's on the "c" drive and not just the main folder.

Does anyone have any code that can do that?

Thanks!

SOLVED

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

David

+2  A: 

You can do it like so:

di.GetFiles("*.*", SearchOption.AllDirectories)
klausbyskov
I'm getting **Access to the path 'C:\System Volume Information' is denied.**? How can i bypass that error message?
StealthRT
If that's an exception, just wrap GetFiles() with a Try...Catch ex as SecurityException. In the Catch do nothing
Eyal
Worked with not showing the error but it still doesn't go through each DIR... See revised code in the OP.
StealthRT
Any suggestions?
StealthRT
+1  A: 

You can specify it as an option in the DirectoryInfo.GetFiles method.

And if you will be deploying on Vista, make sure you handle the access rights properly.

Jas
How would i add that rights properly, Jas?
StealthRT
If I remember correctly, because it's been a while since I dealt with this, you should be invoking a request to elevate your UAC priviledges, which will show that infamous Vista dialog . Take a look at this for example : http://msdn.microsoft.com/en-us/library/bb756929.aspx
Jas
+1  A: 

You need to recursive loop all the folders, I found an article Here!

Emerion