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());
}
}
}
3 Answers 3
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();
}
}
}
-
\$\begingroup\$ Clever ! I like this one. :-) \$\endgroup\$user19024– user190242012年11月14日 19:19:06 +00:00Commented Nov 14, 2012 at 19:19
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.
-
\$\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– Joe2012年11月15日 14:11:55 +00:00Commented 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– avip2012年11月15日 21:47:08 +00:00Commented 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– Joe2012年11月16日 00:37:27 +00:00Commented Nov 16, 2012 at 0:37
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
StreamReader
is for reading text,MemoryStream
is for raw, formatless data, where 'reading a line' has no meaning. \$\endgroup\$