Monday, March 26, 2012

Text File Reverse Read?

Does anyone know of a way to accomplish reading a text file from end to beginning? Like if you wanted to read the last 200 lines from a log file to display on the screen... Can this be done through the framework classes or does this have to be accomplished via a hand made function?I just don't know how to read backwards but you can solve this by writing it backwards and then you can read normally.

To write use:


Dim file As String = "c:\test.txt" 'this must be the name and path of your file
Dim arq As New StreamReader(file)
Dim log_data As String ' this is the content you want to add
Dim cont As String = log_data + arq.ReadToEnd
arq.Close()
Dim arq2 As New StreamWriter("c:\test.txt")
arq2.WriteLine(cont)
arq2.Close()

Then you may read it from the start to the end. You just must to check it's performance.

0 comments:

Post a Comment