C# Writing a File
Keep the work in Pine with Main. Write letters into note.txt with File.WriteAllText. Put tea and a line break into the file. Run with dotnet run. The screen stays empty — the letters go into the file, not onto the terminal. A second demo writes oak the same way and replaces whatever was already inside. Leave the Paths topic for the next page; this lesson only stores short text with File.WriteAllText. Keep the starting public class named Pine.
Store tea and keep the screen empty
The reading page opened a file that already held oak. This page creates or replaces that text from the program itself. Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete leftover File.ReadAllLines demos that print a line from disk. Delete leftover throw new ArgumentException("empty") demos. Delete leftover try and catch demos that print blocked then after. Delete any ReadLine line. The program must not wait for typing.
At the top keep both:
using System;
using System.IO;
Inside Main, store one short line including the line break:
File.WriteAllText("note.txt", "tea\n");
Do not call Console.WriteLine in this first teaching file. The empty screen is part of the lesson: writing is not the same as printing. File.WriteAllText opens the name, writes the text, and finishes the store in one call — you do not add a separate close on this page.
using System;
using System.IO;
public class Pine
{
public static void Main()
{
File.WriteAllText("note.txt", "tea\n");
}
}
Save, then run with dotnet run from the folder where you want note.txt to appear. The terminal stays empty. That is correct. Open note.txt in an editor after the run and you should see tea on the first line. If the terminal prints oak or waits for keyboard input, you are still launching a read leftover — switch to the write teaching file. If you see empty from an ArgumentException, a throwing leftover is still active. If you see blocked or after, a try-catch leftover is still the path you are running. If you see True then 4, a nullable leftover is still active.
File.WriteAllText finishes the store. After the call returns, the letters stay in note.txt when the program ends. This write replaces older content when that name already exists. Creating note.txt when it was missing is part of that same write behavior. You do not call ReadAllLines while storing; that belongs to reading.
Walk the run once. C# starts at Pine.Main. File.WriteAllText opens note.txt for writing, places tea and the line break, and finishes. Nothing is printed. Keep both using lines or File will not resolve. Run from the folder where you want note.txt to appear, so the simple name resolves next to your project. Spelling of the method name matters: WriteAllText. Writing ReadAllLines by mistake turns this into a read demo again.
A later write replaces older letters
A second program starts fresh with the same shape. It still uses File.WriteAllText on note.txt, but the text is oak instead of tea.
using System;
using System.IO;
public class Pine
{
public static void Main()
{
File.WriteAllText("note.txt", "oak\n");
}
}
Run with dotnet run. The screen stays empty again. After this run, note.txt holds oak, not tea. The older line was replaced. That is the contrast with appending tools you are not using on this page — this write starts the file content over for the teaching story. Spelling of the file name must match what you intend to open later: note.txt, not a mistyped twin sitting beside it.
You can confirm with the previous page's read demo if you still have it: reading note.txt after the oak write should print oak. You do not need to paste ReadAllLines into the write teaching file. Keep writing and reading as separate teaching files so each lesson stays clear. One program writes; another program reads; that split keeps each screen honest.
Do not invent a Paths API on this page. You are writing a simple name string "note.txt" next to the project. The next page is about paths as their own topic. Also do not treat a missing folder as today's puzzle — keep the working directory the folder where you run dotnet run. Do not nest a second Main.
Compared with reading on the files page
On the files page, dotnet run printed a line that was already on disk, or stopped with FileNotFoundException for a missing name. On this page, dotnet run puts the letters there itself. Reading and writing stay different jobs. Keep read files nearby only as history when you want to check what landed; run the write teaching file for the write demos.
File.WriteAllText is the new token. Skipping the write and leaving only a ReadAllLines print is a common leftover mistake after a successful build. If dotnet run waits for typing, ReadLine is still in the file — delete it.
What this leaves for paths
This page stores short text with a simple file name beside your project. The next page introduces paths more carefully. Stay with File.WriteAllText here. Do not add path objects as a requirement on these Pine demos yet.
You can write tea into note.txt and see an empty screen while the file holds that line. You can write oak next and replace the older text. Run each kept demo with dotnet run in turn. Open the text file in an editor when you want to confirm what landed. Think of writing as storing letters on disk without printing them. Pine stays the door you open with dotnet run. The next page is paths, where names and locations get their own focus.
Keep leftover ReadAllLines prints out of the write files so the screen cannot show oak from a read when you meant an empty write screen. Keep using System.IO; on both teaching files. Keep the tea write for a clear first store. Keep the oak write for a clear replace. Keep path APIs off this page. Those habits are the checkpoint before you move on. When note.txt shows oak after the second write run, writing a file did its job for this lesson and you are ready for the next topic about paths.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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