1

So like the title of this post says how can i read the last n lines of a file and store them in a List?

Keep it in mind that:

  • the file is big (somewhere between 5 MB and 10 MB)
  • the file may or may not be locked by another thread
  • it is accessed by another thread (a new line is added to file when process finished)

Thank you.

Cristian Szpisjak
  • 2,429
  • 2
  • 19
  • 32

1 Answers1

1

you can use this code:

            List<string> Temp = new List<string>();
            int SkipLinesNum = 8;
            var GetAllFileToVar = File.ReadLines(@"C:\Sahbak\LinesToList.txt").Skip(SkipLinesNum);
            for (int n = 0; n < GetAllFileToVar.Count(); n++)
            {
                Temp.Add(GetAllFileToVar.ElementAt(0));
            }

now you have List of string called "Temp" that contains all lines from n=8. you can edit you "n" number any time, and also get it from the user.

this is the most efficient method, where you do not load all file to memory.

Eliad
  • 140
  • 1
  • 11