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 ?
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With