Question I need help to get all files and folders at specific driver.

Mirace

Member
Joined
Apr 29, 2012
Messages
9
Programming Experience
3-5
Hello !
I just registered here and first i want say hello to all! :)

So, im doing file scanner programm ( I only need code to show all folders and subfolders including all files on D driver ) and i got stucked on very first place. I'm using visual studio 2010, vb.net and my problem is that when i press button nothing happens, not even error occurs!

here is my code

VB.NET:
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

            Dim Joku = System.IO.Directory.GetDirectories("D:\", System.IO.SearchOption.AllDirectories)
            For Each Files In Joku

                Dim Joku2 = System.IO.Directory.GetFiles(Files, "*.*", System.IO.SearchOption.AllDirectories)
                For Each Files2 In Joku2
                    ListBox1.Items.Add(Files2)
                Next

            Next

        Catch ex As Exception

        End Try
    End Sub
End Class
 
Last edited:
Most likely an error is occurring but your code specifically ignores it. The Catch block catches the excption but does nothing about it. Put some code in there to notify you of the exception and you should then see what's going wrong.
 
Thanks,

I tried it allready:
VB.NET:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

            Dim Joku = System.IO.Directory.GetDirectories("D:\", System.IO.SearchOption.AllDirectories)
            For Each Files In Joku

                Dim Joku2 = System.IO.Directory.GetFiles(Files, "*.*", System.IO.SearchOption.AllDirectories)
                For Each Files2 In Joku2
                    ListBox1.Items.Add(Files2)
                Next

            Next

        Catch ex As Exception
            MsgBox(ex)
        End Try
    End Sub
End Class

Still not working, no errors.
 
Ex is a system exception, not a string value.

Try...

Option Strict On

Imports System.IO

Public Class MainForm

    Private Sub btnFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim t As New Threading.Thread(AddressOf FileList)
        With t
            .IsBackground = True
            .Start()
        End With
    End Sub

    Private Sub FileList()
        Dim dir As New IO.DirectoryInfo("C:\")

        For Each fi As FileInfo In dir.GetFiles()
            AddFiles(fi)
        Next

        For Each diSourceSubDir As DirectoryInfo In dir.GetDirectories()
            For Each fi As FileInfo In diSourceSubDir.GetFiles("*.*", SearchOption.AllDirectories)
                AddFiles(fi)
            Next
        Next
    End Sub

    Private Sub AddFiles(ByVal file As FileInfo)
        If Me.ListBox1.InvokeRequired Then
            Me.ListBox1.Invoke(New Action(Of FileInfo)(AddressOf AddFiles), file)
        Else
            Try
                Me.ListBox1.Items.Add(file.FullName)
            Catch ex As IO.PathTooLongException ' Probably should validate here, lazy.
                MessageBox.Show("File path is to long to be displayed.", Application.ProductName)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class


I'm not to sure about a long task like this updating the UI.
 
Ex is a system exception, not a string value.

Try...

Option Strict On

Imports System.IO

Public Class MainForm

    Private Sub btnFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim t As New Threading.Thread(AddressOf FileList)
        With t
            .IsBackground = True
            .Start()
        End With
    End Sub

    Private Sub FileList()
        Dim dir As New IO.DirectoryInfo("C:\")

        For Each fi As FileInfo In dir.GetFiles()
            AddFiles(fi)
        Next

        For Each diSourceSubDir As DirectoryInfo In dir.GetDirectories()
            For Each fi As FileInfo In diSourceSubDir.GetFiles("*.*", SearchOption.AllDirectories)
                AddFiles(fi)
            Next
        Next
    End Sub

    Private Sub AddFiles(ByVal file As FileInfo)
        If Me.ListBox1.InvokeRequired Then
            Me.ListBox1.Invoke(New Action(Of FileInfo)(AddressOf AddFiles), file)
        Else
            Try
                Me.ListBox1.Items.Add(file.FullName)
            Catch ex As IO.PathTooLongException ' Probably should validate here, lazy.
                MessageBox.Show("File path is to long to be displayed.", Application.ProductName)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class


I'm not to sure about a long task like this updating the UI.

Thanks, its working now! But i get acces denied error when its scanning documents folder. How to handle it?
 
Unless you are admin then you will have issues. My advice to you would be to implement a list that handles access denied directories before you start file searching. Create Two lists and then create a method that loops the directories. Each time you managed a successful path add it to the safe list and loop back around. If the path throws an exception add it to the access denied list.

I would also suggest not updating the UI on each file as i shown above. I would add each file to a List(Of FileInfo) and add the range when completed. Targets framework 3.5

Option Strict On

Imports System.IO

Public Class MainForm
    Private m_files As New List(Of FileInfo)

    Private Sub FileList()
        Dim dir As New IO.DirectoryInfo("C:\")

        For Each diSourceSubDir As DirectoryInfo In dir.GetDirectories()
            Try
                For Each fi As FileInfo In diSourceSubDir.GetFiles("*.*", SearchOption.AllDirectories)
                    Try
                        If Not fi.FullName.Length >= 260 Then
                            m_files.Add(fi)
                        End If
                    Catch ex As Exception
                        Debug.WriteLine(ex.Message)
                    End Try
                Next
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        Next

        AddFiles()
    End Sub

    Private Sub AddFiles()
        If Me.ListView1.InvokeRequired Then
            Me.ListView1.Invoke(New MethodInvoker(AddressOf AddFiles))
        Else
            Me.ListView1.Items.AddRange((From file As FileInfo In m_files Select New ListViewItem(file.FullName)).ToArray)
            Me.Text = (Me.ListView1.Items.Count + 1).ToString
        End If
    End Sub

    Private Sub SearcgDirectories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearcgDirectories.Click
        Dim t As New Threading.Thread(AddressOf FileList)
        With t
            .IsBackground = True
            .Start()
        End With
    End Sub
End Class

 
Back
Top