Short Answer
Yes you have to loop line by line.
Details
Here the simplest approach. It is taken from ReadAllLines, File.cs > InternalReadAllLines > ReadLine, StreamReader.cs.
You will see that the reference version handles all line terminator combinations: \r, \n, and \r\n correctly.
ReadLine does not return an extra empty string when the line terminator is \r\n, which is typical in DOS/Windows.
ReadLine also discards any text after the final delimiter.
public static String[] ReadAllLines(this TextReader reader) { String line; List<String> lines = new List<String>(); while ((line = reader.ReadLine()) != null) { lines.Add(line); } return lines.ToArray(); }
While there are reasons to not use ReadAllLines at all, this is what the op asked.
This accepts a TextReader, not just a StreamReader. It supports a StreamReader or a StringReader.
BTW the name StreamReader is an abomination, since it does not read streams, but implements TextReader for files. In contrast a Stream: "Provides a generic view of a sequence of bytes. This is an abstract class." In other words it could be a FileStream - binary stream with possibly no applicable text encoding.
Why Use ReadLine
Text files are post-fix delimited; meaning a new-line terminates each line. Also there are 3 combinations for newlines in common use across Windows, Unix and Mac O/S. Your application may never be ported to another O/S, but it may be expected to read an external file from a foreign O/S.
Split is not equivalent to ReadLine. Split is suited best used for infix delimited strings, such as comma separated lists. It is unsuited for post-fix strings, where the delimiter may be one of three combinations. Split "sees" (parses) \r followed by \n as 2 separate delimiters and returns an empty string. It also returns any text after the final delimiter.
The StringSplitOptions.RemoveEmptyEntries option suggested in some other answers removes all empty lines, including those which were in the original input.
Thus for the input:
line1\r \r line3\r\n
ReadLine returns 3 lines. The 2nd is empty. Split creates 4 strings. (There is an additional string after the last \n.) It then removes the 2nd and the 4th. This is not what ReadAllLines does.
reader.ReadToEnd().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);?