Reading file contents into a string in .Net Compact Framework

I am developing a mobile application with .net compact framework 2.0. I am trying to load the contents of a file into a string object, but somehow I cannot do this. There is no ReadToEnd() method in the System.IO.StreamReader class. Is there any other class that provides this functionality?

+50
c # compact-framework
Jul 27 '11 at 20:31
source share
4 answers
 StringBuilder sb = new StringBuilder(); using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { sb.AppendLine(line); } } string allines = sb.ToString(); 
+96
Jul 27 '11 at 20:33
source share
 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!

+39
Jul 23 2018-12-21T00:
source share

File.ReadAllText(file) what are you looking for?

There's also File.ReadAllLines(file) if you prefer to split it into an array by line.

+7
Jul 27 '11 at 20:34
source share

I do not think file.ReadAllText is supported in the Compact Framework. Instead, try using this streamreader method.

http://msdn.microsoft.com/en-us/library/aa446542.aspx#netcfperf_topic039

This is an example of VB, but pretty easy to translate to C # ReadLine returns null when there are no more lines to read in it. You can add it to the string buffer if you want.

+3
Jul 27 2018-11-21T00:
source share



All Articles