Searching files and folders

Hello everyone..
I have this problem in making a simple search like the windows search to search files and folders.
I want it to search within the specified folder and subfolders inside it...and return all the files matching the query.

Here is what Ive tried.,.

code:--------------------------------------------------------------------------------
Dim s As String = TextBox1.Text
        For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
            ListView1.Items.Add(file)
        Next file
--------------------------------------------------------------------------------
.but the problem is...My program freezes till the search ends unlike the windows search.

Someone help...Thank you so much in advance :-)
godwindotnetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

doobdaveCommented:
Hi there,

The problem you are experiencing is, I believe, related to threading (do a bit of reading up on this topic if you're not familier with the concepts).

Basically what is happening is that your form/UI and the 'search' function are all running in the same thread, which means that everything is executed in a sequential manner, and therefore each function must complete before the code moves on to the next one.

What you need to do is execute the Search function in a separate thread, which means it will go off and do its work, but your UI will remain responsive as it's running in its own thread.

The following code should help you, put it inside the button click event handler, or wherever it is you call the Search function:

Dim thr as System.Threading.Thread
thr = New system.Threading.Thread(AddressOf NameOfYourSearchSub)
thr.Start

Note that your search procedure should be a sub annd not a function and should not take in any parameters, or things become more complicated.

Hope this helps.

Best Regards,

David
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's not ~that~ simple...

You should NOT be updating the man UI interface from within your thread.  So this line may cause problems:

    ListView1.Items.Add(file)

You need to use a Delegate to marshal the call from the Thread back onto the main UI Thread.  I can post an example if you like.

Alternatively you can ~cheat~ by adding a call to Application.DoEvents() inside your loop:

        Dim s As String = TextBox1.Text
        For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
            ListView1.Items.Add(file)
            Application.DoEvents()
        Next file

Just make sure that you don't allow the user to start another search while the code is runing.  This can be achieved by disabling buttons etc., until the operation is complete.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doobdaveCommented:
Good point Idle_Mind, I should've noticed that!
godwindotnetAuthor Commented:
Idle_Mind,That wasnt the perfect solution though
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hi godwindotnet,

Then please give feedback so we know that the proposed solutions either didn't work or still don't perform as well as you want.

Without comments from you we will just assume that they solved the problem.

I'm almost always willing to give examples when they are asked for!

=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.