Writing/Reading Text Files

Let's learn how to read and write text files.

How to read and write text files?

Variables are temporary; once the program is turned off, the data is lost. Writing and Reading to a text file is a permanent way to handle data that needs to be saved when the program is turned off.

The StreamWriter class writes to text files. This class is contained in the System.IO namespace. To use this class, include the following at the top of the page.

using System.IO; // C# 10 comes with ImplicitUsings, meaning the most popular using namespaces such as this one are already built-in so there is no need to declare it

Get hands-on with 1200+ tech skills courses.