get the last line in text file (

i want to kow how to read the last string line in a text file ,then to compare that last line with a string text in a textbox i have , if they equal i dont write the text in the text box  in the file , else if not equal i have to write the text in the text box   after the last line in the text file
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:
Taken from MSDN:
        ' Open the file to read from.
        Dim sr As StreamReader = File.OpenText(path)
        Dim s As String
        Do While sr.Peek() >= 0
            s = sr.ReadLine()

Open in new window

WDAFFAAuthor Commented:
i need to know what (peek) do?
how to earse the last line? or write over it
then how to write at the end after the last line/
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a different method:
        Dim fileName As String = "c:\someFile.txt"
        Dim lines As New List(Of String)
        ' display the last line
        Dim lastLine As String = lines.Item(lines.Count - 1)
        MessageBox.Show("lastLine = " & lastLine)
        ' replace the last line (in memory)
        lines.Item(lines.Count - 1) = "new line here"
        ' add a line (in memory)
        lines.Add("line added to end")
        ' replace the original file
        System.IO.File.WriteAllLines(fileName, lines.ToArray)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Refael AckermannCommented:
I believe Idle_Mind's answer is more complete then mine.

"Peek" allow you to see what the next character in the stream is, but doesn't move the stream pointer forward.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.