Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using following code to read text from a .txt file. But I donot know how to perform search within file and how to read a specific line in the text file, based on search.

VB
Dim vrDisplay = My.Computer.FileSystem.ReadAllText(CurDir() & "\keys.txt")
    MsgBox(vrDisplay)

For example if I want to read the line that contains word "Start", how to do that.

Thanks
Posted

Try this simple solution:

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       'A file named keys.txt contains
       '        afcd b g f
       'rgb          thh h h
       'h hg hg
       '        h(hg)
       '        hg()
       '        h()
       '        hg(hjnthbrggfgn)
       'Start your are ******
       'end no one can do that
       Dim filedata = My.Computer.FileSystem.ReadAllText(CurDir() & "\keys.txt")
       Dim sp() = filedata.Split(vbNewLine)
       Dim IndexOfStart = Array.FindIndex(Of String)(sp, AddressOf CheckStr)
       Dim linecontainigStart = sp(IndexOfStart)
   End Sub

   Function CheckStr(ByVal line) As Boolean
       Dim tmp = InStr(line, "Start")
       If tmp > 0 Then
           Return True
       End If
       Return False
   End Function
 
Share this answer
 
Comments
Furqan Sehgal 31-Jul-11 2:52am    
Aslam bhai,
Thanks for the answer,
Can I have your intro please?
Furqan
Аslam Iqbal 31-Jul-11 2:58am    
Did you get my email ID?
Did you even try to look in Google [^]for how other people do this?

I see quite a few promising links in there. Try some of those, if you are still stuck; come back and someone will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900