foreach (var item in ReadLines("E:\\bigFile.txt"))
{
Console.Write(item);
}
/// <summary>
/// 异步读取每行内容
/// </summary>
/// <param name="fileName">指定文件的绝对路径</param>
/// <returns></returns>
public static IEnumerable<string> ReadLines(string fileName)
{
return ReadLines(delegate
{
return File.OpenText(fileName);
});
}
private static IEnumerable<string> ReadLines(Func<TextReader> provider)
{
using (TextReader reader = provider())
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}