get the last line in text file  (

Posted on 2008-11-07
Last Modified: 2012-05-05
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
Question by:WDAFFA
    LVL 7

    Expert Comment

    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


    Author Comment

    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/
    LVL 85

    Accepted Solution

    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

    LVL 7

    Expert Comment

    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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now