C#
Public
Read All Lines from File
Read a text file into lines and print each line with its index.
#csharp
#file
#text
#beginner
C#
using System;
using System.IO;
string path = "notes.txt";
if (!File.Exists(path))
{
Console.WriteLine("File not found.");
return;
}
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
Console.WriteLine($"{i + 1}: {lines[i]}");
}
Notes
File.Exists avoids a file-not-found crash in beginner examples.