5
\$\begingroup\$

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());
        }
    }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – avip
    Nov 14, 2012 at 17:43
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Joe
    Nov 14, 2012 at 22:16

3 Answers 3

7
\$\begingroup\$

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();
            }
        }
    }
\$\endgroup\$
1
  • \$\begingroup\$ Clever ! I like this one. :-) \$\endgroup\$
    – user19024
    Nov 14, 2012 at 19:19
2
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Joe
    Nov 15, 2012 at 14:11
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – avip
    Nov 15, 2012 at 21:47
  • \$\begingroup\$ If you can elaborate your answer with a quick explanation on the linking, it would be great. \$\endgroup\$
    – Joe
    Nov 16, 2012 at 0:37
2
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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