search for str in file [resolved]
I am kind of new to vb.net, im an old vb6 pro :)
Im having issues writing code to search for a string within a file. Mainly, I really dont know much about the new file handling features of vb.net.
Example:
searchString = "red"
Contents of file to search:
<font color="red">
Any help is greatly appreciated.
Re: search for str in file
I dont have a IDE here but...
VB Code:
Imports System.IO ' for StreamReader
'-----
Public Function SearchFile(FileName as String, SearchString as String) as Boolean
Dim sr as new StreamReader(FileName)
Dim strLines() as String = sr.ReadToEnd.Split(vbcrlf)
sr.Close
sr = nothing
Dim intIndex as Integer '
For Each s as String in strLines
intIndex = s.IndexOf(SearchString )' IndexOf returns -1 if the string not found
If not intIndex = -1 Then
Return True
End If
Next
This simple method returns true if the file contains the SearchString (it does not continue looking after it finds a match.
VB Code:
'usage
Msgbox (SearchFile("c:\test.txt", "red"))
Re: search for str in file