How to use StreamReader to read multiple lines from a text file?

  • Thread starter Thread starter Rechtfertigung
  • Start date Start date
R

Rechtfertigung

Guest
I am trying to use a StreamReader to read First/Last names from a file. The basic operation works which is reading the file and printing the names to screen however I cannot get the names after the first line to be read. I have tried using a While loop to keeping reading the the file contents but it keeps reading the first line and looping forever


Contents of file:

Justin Nickles
John Reves
Robert Onterio
Nick Sanchez
Nia Jules


static void Main(string[] args)
{
List<Students> Classroom101 = new List<Students>();
using (StreamReader StudentNames = new StreamReader("StudentNames.txt"))
{
string CurrentLine = StudentNames.ReadLine();

while (!StudentNames.EndOfStream)
{
List<string> FirstNames = (CurrentLine.Split(' ').Take(1)).ToList();
List<string> LastNames = (CurrentLine.Split(' ').Reverse().Take(1)).ToList();
for (int i = 0; i < FirstNames.Count; i++)
{
for (int l = 0; l < LastNames.Count; l++)
{
Classroom101.Add(new Students() { StudentFirstName = FirstNames, StudentLastName = LastNames[l] });
Console.WriteLine(Classroom101.GetStudentFullName() + "\n");
}
}
}
}
}

Continue reading...
 
Back
Top