我正在使用 .net compact framework 2.0 开发移动设备应用程序。我想要将一个文件的内容加载到一个字符串对象中,但是我一直无法完成。在 System.IO.StreamReader
类中没有 ReadToEnd()
方法。是否有另一个类可以提供这个功能?
我正在使用 .net compact framework 2.0 开发移动设备应用程序。我想要将一个文件的内容加载到一个字符串对象中,但是我一直无法完成。在 System.IO.StreamReader
类中没有 ReadToEnd()
方法。是否有另一个类可以提供这个功能?
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();
}
另一个选项:
string[] lines = File.ReadAllLines("file.txt");
https://gist.github.com/paulodiogo/9134300
简单!
Close()
。让框架为您完成所有操作。 - krillgarClose()
不仅会调用,还会调用Dispose()
,释放资源。 - krillgar您需要的是 File.ReadAllText(file)
吗?
如果您更喜欢将文本按行拆分成数组,则可以使用 File.ReadAllLines(file)
。
ReadToEnd
丢失了,但是它在 .net 2.0 紧凑版中是存在的。 - Brad Christie