9

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.

4

3 回答 3

9

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()
于 2012-07-04T09:58:59.293 回答
8

Tried with a simple text file.

var lines = File.ReadLines("yourfile").Take(300);
readlines[i] = string.Join("-", lines);
于 2012-07-04T09:59:09.287 回答
3

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

于 2012-07-04T09:49:49.433 回答