Public Sub Main()
' Modify this path as necessary.
Dim startFolder = "c:\program files\Microsoft Visual Studio 9.0\VB\"
'Take a snapshot of the folder contents
Dim dir As New System.IO.DirectoryInfo(startFolder)
Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
Dim searchTerm = "Visual Studio"
' Search the contents of each file.
' A regular expression created with the RegEx class
' could be used instead of the Contains method.
Dim queryMatchingFiles = From file In fileList _
Where file.Extension = ".htm" _
Let fileText = GetFileText(file.FullName) _
Where fileText.Contains(searchTerm) _
Console.WriteLine("The term " & searchTerm & " was found in:")
' Execute the query.
For Each filename In queryMatchingFiles
' Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit")
' Read the contents of the file. This is done in a separate
' function in order to handle potential file system errors.
Function GetFileText(ByVal name As String) As String
' If the file has been deleted, the right thing
' to do in this case is return an empty string.
Dim fileContents = String.Empty
' If the file has been deleted since we took
' the snapshot, ignore it and return the empty string.
If System.IO.File.Exists(name) Then
fileContents = System.IO.File.ReadAllText(name)
Open in new window
IT issues often require a personalized solution. With Ask the Experts™, submit your questions to our certified professionals and receive unlimited, customized solutions that work for you.
Take hold of your future.