Ryan Peschel Ryan Peschel - 1 year ago 198
C# Question

ReadAllLines for a Stream object?

There exists a

but not a

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?

Answer Source

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()

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...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download