Delete the last line of a StreamWriter?

12,080

Solution 1

I don't think that a StreamWriter allows you to do this, but perhaps you could make your own stream wrapper which implements this behavior? That is, it would keep the last line in memory and only write it out when another line comes in.

Solution 2

You could just remove the last line after reading in them all:

var lines = System.IO.File.ReadLines(pluginQmlFileName);

// will need changing to remove from the array if using ReadAllLines instead of ReadLines
lines = lines.RemoveAt(lines.Count - 1);

Solution 3

this seems to work with me as well :

//you must attemp to close all open streams of that file if any //if you are using it inside using (StreamWriter sw = .... ) {....} //attempt => sw.close(); //then

FileStream fs = new FileStream(filePath , FileMode.Open, FileAccess.ReadWrite);
fs.SetLength(fs.Length - 1);
fs.Close();

Solution 4

No you can not remove any data from a stream opened by a StreamWriter. StreamWriter is designed to write in a file, so you can not remove anything explicitly.

So have to use an other class to read file content and then since StreamWriter class inherited from TextWriter class you should be able using WriteLine(string) method but this doesn't not make sense since you already have a pretty nice working code.

// read all lines
// ...
var allExceptLast = lines.Take(lines.Length - 1);
foreach(var line in allExceptLast)
{
   writer.WriteLine(line);
}
Share:
12,080
Guillaume Slashy
Author by

Guillaume Slashy

Software Developer

Updated on August 26, 2022

Comments

  • Guillaume Slashy
    Guillaume Slashy almost 2 years

    I know that when we use a StreamWriter this is, by definition, to write in it but the fact is that at some point I can have the obligation to delete the last line of my streamwriter...

    I found the following code (on SO) that works well :

    var lines = System.IO.File.ReadAllLines(pluginQmlFileName);
    System.IO.File.WriteAllLines(pluginQmlFileName, lines.Take(lines.Length - 1).ToArray());
    

    but the thing is that I can't use it in my :

    using (StreamWriter sw = new StreamWriter(pluginQmlFileName, true))
    {
        [...]
    }
    

    section.

    Is there a way to delete the last line in the using {} section or do I have to keep my actual code ?

  • Shadow The Kid Wizard
    Shadow The Kid Wizard over 12 years
    True, but even with TextWriter there's no "RemoveLine" method. Once the text is written, it can't be undone.
  • coder14
    coder14 over 12 years
    Yup, that's how I would do it. But only if I had to write into a SteamWriter. It seems simpler not to do the write in the first place, no?
  • Guillaume Slashy
    Guillaume Slashy over 12 years
    thats a "heavy" solution in my code... :( And about not writing it, see my comment on my question (@scraimer)
  • Guillaume Slashy
    Guillaume Slashy over 12 years
    Doesnt really help with my StreamWriter problem but thats a nice tip!
  • Guillaume Slashy
    Guillaume Slashy over 12 years
    would be extremely hard to use it with my code :( the comment on my question should explain why !
  • Shambhu Kumar
    Shambhu Kumar over 12 years
    It depends what you're doing in between the read and write. If you're reading then writing immediately after and know that you want to remove the last line that it would be worth doing that. Although if not and you end up storing the lines then you could remove the last line just before writing to the file via the StreamWriter. Otherwise it might be worth looking at the TextWriter.
  • Vilx-
    Vilx- over 12 years
    Well, yes, that is a bit heavy. Why didn't you like the idea about storing all the lines in a List and then writing them all out once you're done with the loopings?