在.Net Compact Framework中读取文件内容

我正在开发一个移动设备的.net紧凑框架2.0的应用程序。 我正试图加载文件的内容到一个string对象,但不知何故我无法完成。 System.IO.StreamReader类中没有ReadToEnd()方法。 是否有另一个类提供这个function?

 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(); 
 string text = string.Empty; using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8)) { text = streamReader.ReadToEnd(); } 

另外一个select:

 string[] lines = File.ReadAllLines("file.txt"); 

https://gist.github.com/paulodiogo/9134300

简单!

File.ReadAllText(file)你在找什么?

还有File.ReadAllLines(file)如果你喜欢它分解成一行数组。

我不认为紧凑的框架中支持file.ReadAllText。 尝试使用此StreamReader方法。

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

这是一个VB的例子,但很容易翻译到C#ReadLine返回null,当它没有更多的行读取。 如果需要,可以将其追加到string缓冲区。