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
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.
Open the form designer by clicking the "Design" item under the "Windows" menu heading.
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.
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.
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.
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.
Type the following statement above the "Public Class" text. The "Imports" statement makes the .NET library code available to your program.
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.
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.
Dim d As String Dim f As String Try d = TextBox2.Text For Each f In Directory.GetFiles(d, TextBox1.Text) MsgBox("The file is here.") Next Catch e As System.Exception Debug.WriteLine(e.Message) End Try End Sub
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.
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for