在.Net Compact Framework中将文件内容读取为字符串

lng*_*lng 50 .net c# compact-framework

我正在使用.net compact framework 2.0为移动设备开发应用程序.我试图将文件的内容加载到字符串对象,但不知怎的,我无法完成它.班上没有ReadToEnd()方法System.IO.StreamReader.是否有另一个类提供此功能?

Jet*_*hro 98

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();
Run Code Online (Sandbox Code Playgroud)


Dio*_*ogo 41

string text = string.Empty;
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
{            
    text = streamReader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)

另外一个选项:

string[] lines = File.ReadAllLines("file.txt");
Run Code Online (Sandbox Code Playgroud)

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

简单!


Bra*_*tie 7

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

File.ReadAllLines(file)如果你喜欢它也可以逐行分解.