Search code examples
c#fileulong

Read Line From Text File With More Than Int.Max Number of Lines


I've Googled and looked at several answers but I haven't found a solution for this: I have a .txt file that is comprised of more than int.max number of lines. How do I read a specific line whose number is > int.max? I found this question which has an answer listed as being able to

string line = File.ReadLines(FileName).Skip(14).Take(1).First();

But unfortunately the .Skip() extension method does not accept Long (or in my needed case ULong) parameters.

So I started looking to overload the .Skip extension (or more accurately make my own) to accept a ULong and found this Which didn't help me either as it pertains to Linq to entities.

Please can someone help me understand how to read a specific line from a text file where the line number is > int32.Max

Thanks


Solution

  • You can write your own Skip as an extension method

    public static IEnumerable<T> MySkip<T>(this IEnumerable<T> list, ulong n)
    {
        ulong i = 0;
        foreach(var item in list)
        {
            if (i++ < n) continue;
            yield return item;
        }
    }