how to delete the last line of the text file using c#.

i applicaiton create a text file ..with 2 empty lines at the end of the File

now i just require a method which open the text file and delete the last 2 lines of the text File ..the code in c# is preferred

yaar please help me out with this on ..

thanks in advance

Dani AI

Generated

For : ’s idea (read the file, remove lines, rewrite) is fine — below are two concrete C# approaches that implement that safely. The first is the simplest and works well for small files. The second streams through the file and avoids loading everything into memory (better for large files). Both handle empty lines at the end the same way; a small helper to trim trailing blank lines is shown too.

using System.IO;
using System.Linq;

static void RemoveLastLines(string path, int count)
{
    if (count <= 0) return;
    var lines = File.ReadAllLines(path); // loads whole file into memory
    int keep = Math.Max(0, lines.Length - count);
    File.WriteAllLines(path, lines.Take(keep));
}
using System.IO;
using System.Collections.Generic;

static void RemoveLastLinesStream(string path, int count)
{
    if (count <= 0) return;
    var temp = Path.GetTempFileName();
    try
    {
        using (var sr = new StreamReader(path))
        using (var sw = new StreamWriter(temp))
        {
            var buffer = new Queue<string>();
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                buffer.Enqueue(line);
                if (buffer.Count > count)
                    sw.WriteLine(buffer.Dequeue());
            }
        }
        File.Copy(temp, path, true); // overwrite original; use File.Replace for atomic swap on Windows if desired
    }
    finally
    {
        if (File.Exists(temp)) File.Delete(temp);
    }
}

If the goal is specifically to remove trailing empty lines rather than “the last N lines”, use this helper:

static void TrimTrailingBlankLines(string path)
{
    var lines = File.ReadAllLines(path);
    int last = lines.Length - 1;
    while (last >= 0 && string.IsNullOrWhiteSpace(lines[last])) last--;
    File.WriteAllLines(path, lines.Take(last + 1));
}

Notes: specify an Encoding when reading/writing if the file uses a non-default encoding; test on a copy before changing production files; for concurrent access or high reliability prefer atomic replacement (File.Replace) and keep backups.

Dear
Do Something like this.. you can convert your code to C# easily

Public Shared Function ReadBillFile(ByVal Filename As String) As String
        Try
            Dim sr As StreamReader
            Dim i, linecount As Integer

            sr = File.OpenText(Filename)
            Dim contents As ArrayList
            Dim finalfile As String
            While Not (sr.EndOfStream)
                linecount += 1
                contents(linecount) += sr.ReadLine
            End While
                        sr.Close()

            For i = 1 To contents.Count - 1
                finalfile += contents(i)
            Next
           
           Return finalfile 

        Catch ex As IOException
            MsgBox(ex.Message)
        End Try
    End Function

Note : After that, Edit the existing file and save the finalfile data in it.

Mark as sovled, if it helps you
Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.