Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am writing a simple programme to search recursively through a hard drive and return a list of all present files. I am using this in a simple Anti-Virus application. I have written the following function for searching through the files and adding them to a list box:

VB
'The following function is called with the parameter "C:\" or whatever drive i want it to search
Private Function RunScan(ByVal Folder As String) As String
    Dim sReturn As String
    Dim Path As String
    Dim File As String
    Dim Location As New IO.DirectoryInfo(Folder)
    Dim files As IO.FileInfo() = Location.GetFiles()
    Dim CurrentFile As IO.FileInfo

    sReturn = Folder

    For Each Path In IO.Directory.GetDirectories(Folder)
        sReturn &= ControlChars.CrLf & RunScan(Path)
    Next

    For Each File In IO.Directory.GetFiles(Folder)
        sReturn &= ControlChars.CrLf & File
    Next

    For Each CurrentFile In files
'Add every file searched to ListBox 1
        ListBox1.Items.Add(CurrentFile)
    Next
    Return sReturn
End Function


The Code works perfectly when used to scan a small drive such as a flash drive but as soon as it is used to scan a hard drive of larger size the programme becomes unresponsive and crashes, it does not return any errors even when I try to use Try Catch statements within the code.

If anyone has any ideas I would be very grateful to hear from you, Thanks in advance :)
Posted
Updated 13-Sep-10 6:06am
v2

1 solution

It's obvious that you're using a bunch of memory and you're doing a lot of processing.
VB
ListBox1.Items.Add(CurrentFile)

With that line, you are saving a file info object for every file in the path specified, and you are adding it to a listbox. That could consume a lot of memory. Also, you may want to wait until you are done scanning to add the items to the listbox all at once (I think there is a special way to do that so adding items one by one doesn't slow things down so much).
VB
sReturn &= ControlChars.CrLf & RunScan(Path)
sReturn &= ControlChars.CrLf & File

With these lines, you are concatenating a string to an already large string. This creates an entirely new string and both consume memory until the old string gets garbage collected. You do this EACH TIME you perform the string concatenation. Look into StringBuilder to optimize this and prevent the unnecessary string concatenations. You're program will perform better by a factor of N (where N is the number of files) by doing this.

Also, you said your UI locks up. This is because you are running all this code on the UI thread. If you run it on a new thread and only send updates to the UI thread, your UI will not lock up.
 
Share this answer
 
Comments
TheRadish 14-Sep-10 7:19am    
This is kind of what I feared the problem would be, thanks for the suggestions and thanks for your time, much appreciated :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900