Visual Basic Search text in a file function

Searched many site, and there is no such a function in Visual Basic, so have to create by my own.

Visual Basic is very simple and easy language to catch up for new programmer. Below is the function, which you need input the search text, and file name (it’s full name with the path), Optional parameter ignorecase default value is false. If it’s true, then it will take a little bit longer to compare.

You can easily to change to search in binary file as well. But I think most case is search a string in a text file.

Private Function searchTextInFile(text As String, Filename As String, Optional ignoreCase As Boolean = False) As Boolean
Dim readText As String = File.ReadAllText(Filename)
If ignoreCase Then
readText = readText.ToLower
text = text.ToLower
End If
Return readText.Contains(Trim(text))
End Function

Do’t forget to put imports into your code header.

Imports System.IO


About qianggan

Sr. Software Engineer
This entry was posted in Computers and Internet, 计算机与 Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s