Keavon
Keavon

Reputation: 7532

StreamReader read file and StreamWriter write that result omitting first line?

Using the following code:

string lines = "";
using (StreamReader sr = new StreamReader(@"file.txt"))
{
    lines = sr.ReadLine();
}
using (StreamWriter writer = new StreamWriter(@"file.txt"))
{
    writer.Write(lines); // Change this to skip the first line
}

How can I make it rewrite everything EXCEPT the first line?

Upvotes: 2

Views: 2291

Answers (2)

Selman Genç
Selman Genç

Reputation: 101701

Maybe you can try this:

var lines =  File.ReadLines("file.txt").Skip(1).ToList();
File.WriteAllLines("file.txt",lines);

It will write all the lines to your file except the first line and replace your file content.So basically it will remove the first line from your file.

Upvotes: 3

Blablablaster
Blablablaster

Reputation: 3348

var allLinesExceptFirstOne = System.IO.File.ReadAllLines(filename).Skip(1).ToList();

Upvotes: 2

Related Questions