In native C#, how can I read from the end of a file?
This is pertinent because I need to read a log file, and it doesn't make sense to read 10k, to read the last 3 lines.
To read the last 1024 bytes:
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); } }
1.4m articles
1.4m replys
5 comments
57.0k users