One possible solution would be to get your own stream class and override the read / write / write functions so that you can track new lines yourself. Another possible solution is a method that works with a thread to do what you want:
static int LineNumber(System.IO.Stream s) { byte[] buffer = new byte[s.Length]; s.Read(buffer, 0, (int)s.Length); string text = System.Text.Encoding.ASCII.GetString(buffer); int idx = 0; int line = 0; do { idx = text.IndexOf('\n', idx); if (idx > -1) { line++; if (s.Position <= idx) return line; if (idx < text.Length - 1) idx++; } else { if (line > 0) return line; else break; } } while (true); return 1; }
source share