当我必须始终阅读整个文件时,我通常会这样做:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
有更好/更快的方式吗?
答案 0 :(得分:7)
string content = System.IO.File.ReadAllText(@"your file path");
如果你想获得一行:
string[] lines = System.IO.File.ReadAllLines(@"your file path");
如果你想只读行,直到找到某行,那么使用IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(@"your file path")
{
if (line == ...)
{
break;
}
}