public string MiddleLinesExample() { var path = @"Logs/logfile.txt"; var top = 0L; var bottom = 0L; var start = (new System.IO.FileInfo(path)).Length / 2; // middle of file var lines = new List <string>(); using (var reader = new System.IO.StreamReverseReader(path)) { reader.Seek(start); // set start position reader.ReadLine(); // read and discard (potentially) partial line top = reader.Position(); // position of first line in list while (!reader.EndOfStream && lines.Count < 10) { lines.Add(reader.ReadLine()); } bottom = reader.Position(); // position after last line in last } var result = $"Read position first line: {top}\n"; result += $"Read position after last line: {bottom}\n"; result += $"Number of lines read: {lines.Count()}\n"; result += string.Join("\n", lines); return(result); }
public string LastLinesExample() { var lines = new List <string>(); using (var reader = new System.IO.StreamReverseReader(@"Logs/logfile.txt")) { while (!reader.EndOfStream && lines.Count < 10) { lines.Add(reader.ReadLine()); } } return(string.Join("\n", lines)); }