FSm
FSm

Reputation: 2057

Reading specific number of lines from text file in c#

I'm using the following StreamReader to read from text file

string temp = fs.ReadToEnd ();
readlines[i] = temp;   

I want to read a specific number of lines from the text file (let we say, from line number 1 until line number 300 only), then write the lines into one element of array. Could anyone help please? thanks in advance.

Upvotes: 9

Views: 13090

Answers (3)

Steve
Steve

Reputation: 216273

Tried with a simple text file.

var lines = File.ReadLines("yourfile").Take(300);
readlines[i] = string.Join("-", lines);

Upvotes: 8

Kek
Kek

Reputation: 3195

If you want to skip n first lines and read p lines from there :

var lines = System.IO.File.ReadLines(path).Skip(n).Take(p).ToArray()

Upvotes: 9

JohnnBlade
JohnnBlade

Reputation: 4327

Use the ReadLine method and add a counter and increase it by line and when you hit 300 do a break out of the loop

Upvotes: 3

Related Questions