Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a simple text file setup as a Resource through my application's Properties as a byte[]. In order to access a "Stream.ReadLine" function, I believe I need to make it into a StreamReader. Is there a way to get the following result using only 1 Stream, or else, any tricks to make it better?

List<string> list = new List<string>();

using (Stream s = new MemoryStream(myFile))
{
    using (StreamReader sr = new StreamReader(s))
    {
        while (!sr.EndOfStream)
        {
            list.Add(sr.ReadLine());
        }
    }
}
share|improve this question
2  
Why would you hold your file as byte[] in the first place? That's the reason code looks clumsy - StreamReader is for reading text, MemoryStream is for raw, formatless data, where 'reading a line' has no meaning. –  avip Nov 14 '12 at 17:43
    
I use the built-in "Properties/Resources" to store my content. This automatically exposes my file as a byte[]. I didn't ask for this :) –  Joe Nov 14 '12 at 22:16
add comment

3 Answers 3

up vote 7 down vote accepted

Not without having to reinvent a lot of other things and make it more complex, no. This solution looks to be the best to me. You even use using correctly, which is one of my frequent nitpicks. However, if you want to simplify that bit of code, you might be able to help it with an extension method like so:

        List<string> list;

        using (var s = new MemoryStream(myFile))
        {
            list = s.ReadLines().ToList();
        }

...

    public static IEnumerable<string> ReadLines(this Stream s)
    {
        using (var sr = new StreamReader(s))
        {
            while (!sr.EndOfStream)
            {
                yield return sr.ReadLine();
            }
        }
    }
share|improve this answer
    
Clever ! I like this one. :-) –  user19024 Nov 14 '12 at 19:19
add comment

EDIT: So this is the famous "how do I read my embedded resource".

        using (var sr = new StreamReader(Assembly.GetExecutingAssembly().
                GetManifestResourceStream("MyNamespace.MyFile.Ext"))) 
        {
            return sr.ReadToEnd(); // or whatever else suits you
        }

You can also link instead of embedding.


[this section is now irrelevant]

I'd go with:

public static IEnumerable<string> ReadLines(byte[] raw)
{
     using (var sr = new StreamReader(System.Text.ASCIIEncoding.ASCII.GetString(raw)))
     { // or whatever encoding you're actually using...
         return sr.ReadToEnd().Split(Environment.NewLine.ToCharArray());
     }
}

If only to make clear that converting byte[] to string should be done using explicit encoding.

share|improve this answer
    
Interesting. There are so many ways to put content in an application that it's difficult to find the right approach. Some of them are really easy in code but difficult in xaml, sometime it's the opposite. I typically set my files as "Resources", but I wanted to try something new using the application's properties instead. –  Joe Nov 15 '12 at 14:11
1  
If you link a file, end user can edit or rename it, as it just lays in the Resources folder. On the other hand, it's an 'offline' content that you can change without rebuilding your app. If you embed it's safer in that sense, but any change requires rebuild. + your .exe becomes fatter. –  avip Nov 15 '12 at 21:47
    
If you can elaborate your answer with a quick explanation on the linking, it would be great. –  Joe Nov 16 '12 at 0:37
add comment

You can go with one more builtin approach.

You can use:

string content = System.IO.File.ReadAllText(someFilePath);

That actually creates one SteramReader and call reader.ReadToEnd();

Or if you need lazy one, you can use

foreach(string line in System.IO.File.ReadLines(someFilePath))
{
    // do something with line
}

Second approach creates internal StreamReader that reads one line for each iteration. It should be used in foreach because it needs to be disposed at the end.

PS. Sorry, I missed that you are talking about data from Assembly resources

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.