Reading file content to string in .Net Compact Framework

回眸只為那壹抹淺笑 提交于 2019-11-28 07:08:18
Jethro
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();
}

Another option:

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

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

Simple!

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!