Another option is to use the
File.ReadLines
method (see
File.ReadLines Method (System.IO) | Microsoft Docs[
^]).
You can then either use a
foreach
loop to iterate through the lines, like so:
foreach(string line in File.ReadLines("myFile.txt"))
{
}
Or you can use LINQ to populate an array directly (make sure to include a using for System.Linq). For example, to make an array from a file where each line contains a single integral number, you could do the following:
int[] myNumbers = File.ReadLines("myFile.txt")
.Select(line => int.Parse(line))
.ToArray();