' 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) 
Imports System
Imports System.IO
Imports System.Text

Public Class MainClass
    Public Shared Function ReadLastNLines(ByVal fnm As String, ByVal nolin As Integer) As String()
        Dim fs As FileStream = New FileStream(fnm, FileMode.Open, FileAccess.Read)
        Dim l As Long = fs.Length
        Dim n As Integer = 3000
        Dim ix As Long = l - n
        If ix < 0 Then
            n = n + CType(ix, Integer)
            ix = 0
        End If
        fs.Seek(ix, SeekOrigin.Begin)
        Dim b(n-1) As Byte
        fs.Read(b, 0, n)
        fs.Close
        Dim nl As Integer = n
        Dim i As Integer
        For i = 0 To nolin-1
            nl = nl - 2
            While nl >= 0 AndAlso Not (b(nl) = 10)
                nl = nl - 1
            End While
            nl = nl + 1
        Next
        Dim sr As StreamReader = New StreamReader(New MemoryStream(b, nl, n - nl), Encoding.Default)
        Dim res(nolin-1) As String
        For i = 0 To nolin-1
            res(i) = sr.ReadLine
        Next
        sr.Close
        Return res
    End Function
    Public Shared Sub Main(ByVal args As String())
        Dim lins As String() = ReadLastNLines("C:\big.txt", 3)
        For Each lin As String In lins
            Console.WriteLine(lin)
        Next
    End Sub
End Class 
