I am developing an application for mobile devices with the .net compact framework 2.0. I am trying to load a file's content to a string object, but somehow I can't get it done. There is no ReadToEnd() method in the System.IO.StreamReader class. Is there another class that provides this functionality?
50
votes
Please post the code you have so far.
- Nick Heidke
4 Answers
99
votes
41
votes
string text = string.Empty;
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
{
text = streamReader.ReadToEnd();
}
Another option:
string[] lines = File.ReadAllLines("file.txt");
https://gist.github.com/paulodiogo/9134300
Simple!
7
votes
File.ReadAllText(file) what you're looking for?
There's also File.ReadAllLines(file) if you prefer it broken down in to an array by line.
3
votes
I don't think file.ReadAllText is supported in the compact Framework. Try using this streamreader method instead.
http://msdn.microsoft.com/en-us/library/aa446542.aspx#netcfperf_topic039
It's a VB example, but pretty easy to translate to C# ReadLine returns null when it has no more lines to read. You can append it to a string buffer if you want.