i'm working in a .NET Framework 4.8 Environment where i want to read the last line of a text file, similiar to a log file. As the file is continously growing i need to ocassionaly read the last line programatically.
I created a "tail"-like script, which will read the whole file by line as a string array and return X amount of lines from the end.
dim filePath = "C:\file.xy"
dim linesCount = 5 'amout of lines i want to read
dim lines = File.ReadAllLines(filePath)
dim startLineIndex = lines.length - linesCount
for index = startLineIndex to lines.length - 1
dim value = lines(index)
result = result & cstr(value) & Environment.NewLine
next
This naive take on a tail function will get slower, the longer the file gets. How can i receive the last lines of the file without reading the whole document before?
thanks for helping me out!