blah blah blah is here! blah blah » Close

up1down
link

hi i have a text file with 1000 lines and i wand to delete the last line each time i press a button!
is any function to delete the last line??

thanks

foamy
2499

Could you post your existing code?

last answered 2 years ago

1 answers

up2down
link

since they are 1000 lines "which is not that high unless the line has many bytes", read all the lines and write them without the last line.

string myfile="C:\myFile.txt";//any file name
System.Collections.Generic.List<string> newLines = new System.Collections.Generic.List<string>();
newLines.AddRange(System.IO.File.ReadAllLines(myfile));
newLines.RemoveAt(newLines.Count - 1);
System.IO.File.WriteAllLines(myfile, newLines.ToArray());

lios
30

yes but the file have 1000 for start and i beleve the max lines will be 6000-8000

vulpes
17279

You can improve performance by only writing back the remaining lines to the file when the application is about to close.

lios
30

and if the application dont close correctly, then the file is brocken???

Feedback