Search code examples
c#stringfiletextstreamreader

How to read specific lines from a text file a store them as strings. c#


i want to read lines 4 , 5 and 6 from the text file and store them as separate strings. this is my code which justs prints out each line in the text file.

ReadFromFile(@"C:\Users\Eoghan\Documents\Assign02_Data.txt"); 


 static void ReadFromFile(string aTextFile)
    {
        System.IO.StreamReader InputFile = new System.IO.StreamReader(aTextFile);
        string s;
        while (!InputFile.EndOfStream)
        {
            s = InputFile.ReadLine();
            Console.WriteLine(s);
        }
    }

Solution

  • List<string> lines = File.ReadLines(filename).Skip(3).Take(3).ToList();