Searching files and folders

Posted on 2006-04-13
Last Modified: 2010-04-23
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.,.

Dim s As String = TextBox1.Text
        For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
        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 :-)
Question by:godwindotnet
    LVL 8

    Expert Comment

    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)

    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,

    LVL 85

    Accepted Solution

    It's not ~that~ simple...

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


    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)
            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.
    LVL 8

    Expert Comment

    Good point Idle_Mind, I should've noticed that!

    Author Comment

    Idle_Mind,That wasnt the perfect solution though
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now