Click here to Skip to main content
15,906,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
message removed Please Delete this message!!
Posted
Updated 7-Jan-12 12:06pm
v3
Comments
Wendelius 5-Jan-12 18:13pm    
Pre tags added

1 solution

Based on the first impression of your code: Accessing properties of an UI control is always slow. Since you're iterating through files , in my opinion you don't have to set the texts etc to the controls, use variables instead. This also applies to textbox1 and ...2. Since textbox 2 is 'static in during your iteration, take the value from the Text property to a string variable and use that in your loop.

Also since you fill a list box in your code, don't add the items as you go but gather them into a collection and after the loop, use ListBox.ObjectCollection.AddRange Method[^]

Addition:

Most likely the code I wrote is wrong since I didn't know the whole idea for the code, but perhaps something like the following:
VB
...
        Dim text1 As String
        Dim text2 As String
        Dim diTop As New System.IO.DirectoryInfo(ComboBox1.SelectedItem)
        Try
            ' Enumerate all subdirectories.
            For Each di In diTop.EnumerateDirectories("*")
                Try
                    ' Enumerate each file in each subdirectory.
                    For Each fi In di.EnumerateFiles("*", System.IO.SearchOption.AllDirectories)
                        text1 = getMd5Hash(fi.FullName)
                        If text2.Contains(text1) Then
                            itemList.Add(fi.FullName)
                        End If
                        Try
                            ' Catch unauthorized access to a file.
                        Catch UnAuthFile As UnauthorizedAccessException
                            Console.WriteLine("UnAuthFile: {0}",
                                                UnAuthFile.Message)
                        End Try
                    Next
                    ListBox2.Items.AddRange(itemList.ToArray())
                    ' Catch unauthorized access to a subdirectory.
                Catch UnAuthSubDir As UnauthorizedAccessException
...
 
Share this answer
 
v2
Comments
Wendelius 6-Jan-12 13:25pm    
Answer updated
Wendelius 7-Jan-12 14:50pm    
I added the list of string just as an example. The element of the generic list can be anything, but if you add just strings to the list box then a generic list of strings should be sufficient. The point is just that you don't have to add the items to the list box one by one since that's slow.
Wendelius 7-Jan-12 14:52pm    
That's probably because of my example. Most likely I didn't initialize the text2 (or text1) so before the loop, set the text 2 to contain a proper string value you want to use in the comparison.
Wendelius 7-Jan-12 14:53pm    
Unfortunately, that's something I cannot do. To handle the null errors etc. use the debugger and always before you execute a line of code, investigate the values of the variables to get a good understanding, what contains null etc. I'm sure that you will find the cause of the problem in no time :)

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