I got a software that generate a txt file, and adds some data in it, line by line.
I want a second program to read the data in this file, and I was wondering, is there a way to only read the latest one, or only read when a new line is added?
If you want to read the end of a text file then you pretty much have to read all of it and simply discard the parts you don't need. Reading the last line is easy.
var lastLine = System.IO.File.ReadLines(filePath).Last();
That will read the whole file line by line and return the last one, discarding the rest. Note that that code calls ReadLines and not ReadAllLines. The end result would be the same but there is an important difference. While ReadLines reads the file one line at a time and discards the previous when moving onto the next, ReadAllLines will read the entire file in one go and put the results into an array. That latter behaviour is good if you want random access to the whole file but is of no advantage if you just want one line and is a particular problem for big files, as the array will take a large chunk of memory.
If you might want several lines from the end of the file then you would have to know how many you didn't want. How you might do that in this case is up to you but let's assume that you know that there were previously N lines in the file and you want all the new ones after that:
var lines = System.IO.File.ReadLines(filePath).Skip(N).ToArray()
That will read the file and discard the first N lines, then return an array containing the rest.
If you want to know when a change is made to a file then you should look into the FileSystemWatcher component. Just be aware that that component will detect when a change starts, not when it finishes. That means that you may detect a change and try to react to it while the file is still being written to, so you may need to allow for that. To do that you would catch the exception that is thrown when you try to open a locked file and then implement a delay before trying again, probably doing that in a loop until it succeeds or it fails a predetermined number of times.
I should point out that if you did want the last N lines from a file but didn't know how many lines the file contained then there are ways you can do that. Firstly, you could call ReadAllLines instead of ReadLines, get the number of lines in the resulting array and use that to determine how many lines to skip, e.g.
var allLines = File.ReadAllLines(filePath);
var lastNLines = allLines.Skip(allLines.Length - N).ToArray();
That would be fine for small files but, as I said, can be an issue for large files. An alternative would be to read the file line by line yourself and always keep the last N lines read, e.g.
var lines = new Queue<string>();
using (var file = new StreamReader(filePath))
{
while (!file.EndOfStream)
{
lines.Enqueue(file.ReadLine());
if (lines.Count > N)
{
// Discard the oldest line.
lines.Dequeue();
}
}
}
var lastNLines = lines.ToArray();
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.