How to Do a Windows Search in VB

Written by darrin koltow
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Do a Windows Search in VB
Executing the .NET library's GetFiles function with a folder name returns all files in the folder. (binary world image by Attila Toro from

Microsoft's Visual Basic (VB) programming language and environment has several functions for interacting with the Windows operating system. Among these functions are those for performing file searches. Executing these searches with VB involves writing program code that invokes existing functions in code libraries. The .NET framework that ships with VB contains these libraries. Writing programs that execute Windows searches in VB provides a quick introduction to .NET programming, and allows you to customise the default Windows search.

Skill level:

Things you need

  • Visual Basic

Show MoreHide


  1. 1

    Open VB and create a new project in which to program your search application by pressing "Control-N." Click the link that reads "Windows form application" to create an application that you'll customise with program code to perform a Windows search.

  2. 2

    Open the form designer by clicking the "Design" item under the "Windows" menu heading.

  3. 3

    Drag a label control from the control tool box at screen bottom onto the form. Right-click the label and select "Properties," then type "Folder name" in the "Text" item of the "Properties" window.

  4. 4

    Create another label as you did the first one. Drag it to a location directly under the first label, and use step 3's instructions to give the label the text "File name." These two labels prompt your program's user to enter folder and file names that your program will use to do the Windows search.

  5. 5

    Drag a text box control to the right of the second label, and drag another text box to the right of the first one. The user will enter the search parameters (i.e. folder and filenames) in these boxes.

  6. 6

    Drag a button control from the tool box onto the form, below the existing controls. Double-click the button to display the window for entering the program code to execute the search.

  7. 7

    Type the following statement above the "Public Class" text. The "Imports" statement makes the .NET library code available to your program.

    Imports System.IO

  8. 8

    Paste the following statement above the "End Sub" statement in the "Button1_Click" subroutine. This statement calls on your program's main routine, which you'll write next.


  9. 9

    Paste the following code below the "End Sub" line of the "Button1_Click" subroutine. This subroutine works by calling the .NET library's GetFiles function to search Windows for the file entered by the user.

    Sub WinSearch()

        Dim d As String
        Dim f As String
            d = TextBox2.Text
            For Each f In Directory.GetFiles(d, TextBox1.Text)
                MsgBox("The file is here.")
        Catch e As System.Exception
        End Try
    End Sub
  10. 10

    Run the program by pressing "F5." Type a folder name in the form's top text box. Type a file name in the lower text box.

  11. 11

    Press the form's button to run your program code, which will execute the search for the file you specified. The program will display no message if it didn't find the file. It will display "The file is here." if it does find the file.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.