Loading...

C# Reading a File

Prepare a plain text file named note.txt in the same folder as your program. Put one short line inside it: oak. Keep the work in Pine with Main. Read that file with File.ReadAllLines, print the first line, and run with dotnet run. The screen should show oak. A second demo opens missing.txt instead and stops with FileNotFoundException. Leave writing a file with C# for the next page — create note.txt by hand in an editor for this lesson. Keep the starting public class named Pine.

Read a line that is already on disk

Earlier pages stopped with exceptions you caught or threw. This page reaches letters that live in a file after the program ends. Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete leftover throw new ArgumentException("empty") demos. Delete leftover try and catch demos that print blocked then after. Delete leftover bare slots[5] stops. Delete any ReadLine line that waits for typing. The program must not wait for keyboard input.
At the top keep both:
using System;
using System.IO;
Inside Main, read every line from the disk name into an array:
string[] lines = File.ReadAllLines("note.txt");
Print the first line:
Console.WriteLine(lines[0]);
Before you run, create note.txt beside the project files and type exactly oak on the first line. Save the text file. The C# program does not create that text file on this page.

Pine prints oak from note.txt

csharp
using System;
using System.IO;

public class Pine
{
    public static void Main()
    {
        string[] lines = File.ReadAllLines("note.txt");
        Console.WriteLine(lines[0]);
    }
}

Save, then run with dotnet run from the folder that holds both the project and note.txt. The screen shows oak. File.ReadAllLines opened the disk name and filled the array. Index zero holds the first line. The program should not pause for keyboard input. If the terminal waits for you to type, you still have ReadLine in the file — delete it. If you see empty or a stack from ArgumentException, a throwing leftover is still running — switch to the file-read teaching file. If you see blocked then after, a try-catch leftover is still active. If you see 2 alone from slots, an exceptions leftover is still the path you are running. If the screen shows True then 4, a nullable leftover is still active — replace it with the file-read demo.
Walk the happy path once. C# starts at Pine.Main. File.ReadAllLines opens note.txt. The array holds each line. WriteLine shows oak from index zero. Nothing else should print. If the folder has no note.txt, you will hit the missing-file story early — create the text file first, then rerun. Put note.txt in the same working folder you use for dotnet run. A file that exists in another folder under a different name will not help this demo.
ReadAllLines here means lines from the file on disk, not a prompt for the user. Keep the story straight: the letters oak live in the text file; the C# class only reads them. Keep using System.IO; or File will not resolve. Keep using System; for Console.WriteLine. Spelling of the method name matters: ReadAllLines. Writing ReadAllText is a different method; this page's teaching files stay with ReadAllLines and lines[0] so the first-line story matches the quiz. If note.txt holds more than one line, index zero is still the first line only — that is enough for this lesson.

📊 File read pieces on this page

Piece What it does on this page
File.ReadAllLines opens a disk name and returns a string array of lines
lines[0] the first line from that file
FileNotFoundException the stop when the disk name is missing

A missing name throws FileNotFoundException

A second program keeps the same shape but points at a name that is not on the machine: missing.txt. Do not create that file. The run should stop with FileNotFoundException.

Pine missing file throws

csharp
using System;
using System.IO;

public class Pine
{
    public static void Main()
    {
        string[] lines = File.ReadAllLines("missing.txt");
        Console.WriteLine(lines[0]);
    }
}

Run with dotnet run. This time the program does not print oak. The terminal reports FileNotFoundException. You already practiced catch earlier; here the teaching point is recognizing that a missing file name stops the read. Spelling of the exception type matters when you talk about it: FileNotFoundException. Saying only "error" is too vague for this lesson. Notice the project may still build before you run — a missing disk name is a run-time stop, not a missing semicolon.
If you accidentally create missing.txt, delete it or rename the string back to a name that is absent. Prefer the successful note.txt demo when you want a quiet screen with oak. Prefer the missing-name demo only to see FileNotFoundException. Switch files deliberately so the happy read and the missing-name stop are not mixed in one confused Main while you study. Do not leave throw new ArgumentException("empty") as the only path in the teaching file; that stop belongs to the throwing page, not to reading a disk name.
Do not invent a C# write on this page to create note.txt. Creating the text file in an editor is enough for reading practice. The next page is writing a file with C# so a program can store letters itself. Paths and more file tools come later; this page keeps one clear read and one clear missing-name stop. Do not nest a second Main. Keep both using lines on the teaching files. Keep leftover catch demos out so the screen cannot show blocked then after when you meant oak or a missing-file report.

Compared with throw on the throwing page

On the throwing page, dotnet run caused a stop with ArgumentException when cups were empty. On this page, the stop for a missing file is FileNotFoundException, and the happy path prints a line from disk. Keep throwing files only as history if they still sit nearby; run the file-read teaching file for the demos so the screen matches this lesson.
Array crashes, catch demos, and cups throws stay history. Today the new tokens are System.IO, File.ReadAllLines, lines[0] from disk, and FileNotFoundException.

You can read note.txt with File.ReadAllLines and see oak. You can open missing.txt and see FileNotFoundException. Run each kept demo with dotnet run in turn. Create note.txt by hand beside the project. Think of a file read as pulling letters that already live on disk. Pine stays the door you open with dotnet run. The next page is writing a file, where a program stores text itself.
Keep the successful read for a quiet screen. Keep the missing-name demo to see the stop. Keep C# writing off this page. Those three habits are the checkpoint before you move on. When oak appears after dotnet run with note.txt present, reading a file did its job for this lesson and you are ready for the next topic about writing files.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top