从文件末尾开始读取,类似于tail

2024-01-14

在本机 C# 中,如何从文件末尾读取内容?

这是相关的,因为我需要读取日志文件,而读取 10k、读取最后 3 行是没有意义的。


要读取最后 1024 个字节:

using (var reader = new StreamReader("foo.txt"))
{
    if (reader.BaseStream.Length > 1024)
    {
        reader.BaseStream.Seek(-1024, SeekOrigin.End);
    }
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从文件末尾开始读取,类似于tail 的相关文章