// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Text; class MainClass { public static string ReadLastLine(string fnm) { FileStream fs = new FileStream(fnm, FileMode.Open, FileAccess.Read); long l = fs.Length; int n = 1000; long ix = l - n; if(ix < 0) { n = n + (int)ix; ix = 0; } fs.Seek(ix, SeekOrigin.Begin); byte[] b = new byte[n]; fs.Read(b, 0, n); fs.Close(); int nl = n - 2; while(nl >= 0 && b[nl] != '\n') nl--; nl++; return Encoding.Default.GetString(b, nl, n - nl); } public static void Main(string[] args) { Console.WriteLine(ReadLastLine(@"C:\test.txt")); } }