CodShot
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.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support