Daniel van wolf
Daniel van wolf

Reputation: 403

How do i read a text files lines and remove the first line?

string[] files = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
foreach (string file in files)
{

}

I want to to remove from the file UploadedVideoFiles.txt the first line.

Upvotes: 0

Views: 105

Answers (4)

imlokesh
imlokesh

Reputation: 2729

var lines = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", lines.Skip(1));

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

Old school (without LINQ) is to start iterating at index 1.

for (int i = 1; i < files.Length; ++i)
{
   // do something with files[i], which is a line in the file.
}

Upvotes: 0

Glen Thomas
Glen Thomas

Reputation: 10764

Use Enumerable.Skip Linq extension method:

string[] files = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt").Skip(1).ToArray();
File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", files);

Upvotes: 0

Eldar Dordzhiev
Eldar Dordzhiev

Reputation: 5135

Using LINQ is the best approach in this case:

foreach (string file in files.Skip(1))

Upvotes: 3

Related Questions