StreamReader.ReadLine() starting from the end of the stream

c#, parsing

Solution

Funny you should mention it - yes I have. I wrote a `ReverseLineReader` a while ago, and put it in MiscUtil.

It was in answer to this question on Stack Overflow - the answer contains the code, although it uses other bits of MiscUtil too.

It will only cope with some encodings, but hopefully all the ones you need. Note that this will be less efficient than reading from the start of the file, if you ever have to read the whole file - all kinds of things may assume a forward motion through the file, so they're optimised for that. But if you're actually just reading lines near the end of the file, this could be a big win :)

(Not sure whether this should have just been a close vote or not...)

Problem

I'm working in C#/.NET and I'm parsing a file to check if one line matches a particular regex. Actually, I want to find the last line that matches. To get the lines of my file, I'm currently using the System.IO.StreamReader.ReadLine() method but as my files are very huge, I would like to optimize a bit the code and start from the end of the file. Does anyone know if there is in C#/.NET a similar function to ReadLine() starting from the end of the stream? And if not, what would be, to your mind, the easiest and most optimized way to do the job described above?

Original source

Related problems