Reading a File Backwards Quickly in C#

The classes in .NET are structured to read files from the beginning to end. Hard drives are structured like that, sometimes though there is a dire need to read a file (commonly a large file) from the end up, just to see if there are new lines of data. This helps save a lot of memory as well as speed as opposed to reading a whole file into memory, simply to read the bottom five lines.

With the following code you can read a file backwards:

var lines = File.ReadAllLines(textFile).Reverse();foreach (string line in lines){// This will step through each line of the file, from the bottom to the top}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts