Suppose a soccer application contains a lineup note:
string lineupNote = "Jordan starts at forward.";
At this moment, the value belongs to the running program.
To preserve it after the program closes, the application can write the text to a file.
A simple C# API for this purpose is:
System.IO.File.WriteAllText
An instructional example is:
string lineupNote = "Jordan starts at forward.";
System.IO.File.WriteAllText("lineup.txt", lineupNote);
Read the second statement as:
Write all of the text stored in
lineupNoteto the file namedlineup.txt.
The first argument identifies the file.
The second argument provides the text to write.
After the write, the file contains:
Jordan starts at forward.
The file does not contain the C# variable itself.
It contains the text value the program chose to write.
This distinction matters.
The runtime variable and the persistent file are different representations in different locations.
WriteAllText writes the supplied text as the file's complete content.
If the file does not exist, the operation can create it.
If it already exists, this operation replaces its previous text with the newly supplied text.
That makes the method convenient for a simple introductory example where the application wants one current saved value.
Appending additional content is a different operation and is outside the current focus.
A filename such as:
"lineup.txt"
is simple for explanation, but an actual application may need a specific course-provided path or location.
Use the path supplied by the current project.
Do not move storage to another folder or hardcode a new system location simply because it works on one computer.
The program can prepare the text first.
For example:
string playerName = "Jordan";
string position = "Forward";
string lineupNote = playerName + " starts at " + position + ".";
Then:
System.IO.File.WriteAllText("lineup.txt", lineupNote);
The important sequence is:
Create or gather information
↓
Create the text to store
↓
Write the text to the file
After writing:
System.IO.File.WriteAllText("lineup.txt", lineupNote);
the file does not remain automatically synchronized with the variable.
If the program later changes:
lineupNote = "Casey starts at goalkeeper.";
the file still contains the earlier text until another write occurs.
A write captures the text supplied at the time of the operation.
Conceptually:
lineupNote in memory
↓
File.WriteAllText(...)
↓
lineup.txt on persistent storage
The code implements the write action you previously modeled with an activity diagram.
A program can display:
Jordan starts at forward.
without writing a file.
It can also write that text to a file without showing the text on screen.
Display and persistence are different responsibilities.
Do not assume visible output has automatically been saved.
For this module, focus on one simple idea:
System.IO.File.WriteAllText(path, text);
The program has text in memory.
The method writes that text to persistent storage.
Later courses can add:
Those are not needed to understand the basic movement of information from memory to a text file.