Home > DeveloperSection > Forums > Delete the first line of a text file without reading it
marcel ethan
marcel ethan

Total Post:105

Posted on    January-30-2014 11:03 PM

 C# C# 

 1 Reply(s)
 896  View(s)
Rate this:

I'm working with a very large text file (around 70 thousand lines) and I want to remove the top line.

Clearly, loading the entire thing into memory, deleting the top line, then re-writing the whole thing again is inefficiant:

var lines = File.ReadLines(accountFileLocation.Text).Skip(1);

File.WriteAllLines("output.txt", lines);

Is there any other way to do it?

Pravesh Singh

Total Post:411

Posted on    January-30-2014 11:29 PM

Hi Marcel,

Try this:

List lines = File.ReadAllLines(Filename).ToList(); And remove the [0] index from the list create file again

File.WriteAllLines(Filename, lines.ToArray());

Don't want to miss updates? Please click the below button!

Follow MindStick