Search code examples
c#filetextstream

ReadAllLines for a Stream object?


There exists a File.ReadAllLines but not a Stream.ReadAllLines.

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt"))
using (StreamReader reader = new StreamReader(stream))
{
    // Would prefer string[] result = reader.ReadAllLines();
    string result = reader.ReadToEnd();
}

Does there exist a way to do this or do I have to manually loop through the file line by line?


Solution

  • You can write a method which reads line by line, like this:

    public IEnumerable<string> ReadLines(Func<Stream> streamProvider,
                                         Encoding encoding)
    {
        using (var stream = streamProvider())
        using (var reader = new StreamReader(stream, encoding))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
    

    Then call it as:

    var lines = ReadLines(() => Assembly.GetExecutingAssembly()
                                        .GetManifestResourceStream(resourceName),
                          Encoding.UTF8)
                    .ToList();
    

    The Func<> part is to cope when reading more than once, and to avoid leaving streams open unnecessarily. You could easily wrap that code up in a method, of course.

    If you don't need it all in memory at once, you don't even need the ToList...