<分区>
当我必须始终阅读整个文件时,我通常会这样做:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
有更好/更快的方法吗?
标签 c# .net performance
<分区>
当我必须始终阅读整个文件时,我通常会这样做:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
有更好/更快的方法吗?
最佳答案
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;
}
}
关于c# - 打开文件和读取内容的最可靠方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6803945/