Lesson 3 - Working with text files in C# .NET
In the previous lesson, Introduction to working with files, we got acquainted with writing privileges in Windows operating systems. The simplest way to store application data on the hard drive is to use text files. I'm sure you all have already encountered files with the .txt extension. The text is simply stored there on multiple lines. Lines are separated by special characters, which are unfortunately OS-specific. Fortunately, C# solves this problem for us.
Note: On different projects online, you may encounter using different classes and approaches for writing into files. The .NET framework was developed over time and provides older, more complicated structures for backward compatibility's sake. I'll show you the simplest and latest approaches.
Writing text to a new file
First, let's create a new text file and write something into it. Create a new project (a console application) and name it TextFiles. The .NET framework provides the StreamWriter class for us which is used to write into text files.
Now, go ahead and add using System.IO
into the source code.
Next, we'll create a using block and create a new StreamWriter instance inside.
As we already know from previous lessons, using
will automatically
close the file once the reading/writing is finished. We'll specify the path to
our file in the constructor:
using (StreamWriter sw = new StreamWriter(@"file.txt")) { }
Our StreamWriter now points to the appropriate file. We write a new line in text files using the WriteLine() method. Once we're done writing, we'll have to call the Flush() method, which takes care of emptying the buffer. We won't have to deal with any of that, all we have to remember is that any written lines may remain in the buffer memory for a while, so we force to write them using the Flush() method.
At this point, the code looks something like this:
using (StreamWriter sw = new StreamWriter(@"file.txt")) { sw.WriteLine("The first line"); sw.WriteLine("This text is on the second line"); sw.WriteLine("And the third one."); sw.Flush(); }
Once we run it, a file named file.txt will be created in our project folder, specifically, in the bin/debug folder. We have already gone over how to handle file path and writing privileges in C# .NET correctly, so we won't deal with any of that here so as to keep the code simple. As you can see, the file now exists and contains the text we designated:

Appending text to an existing file
If the file doesn't exist, the code above will create it. If it does
exist, it will be overwritten. This behavior can be altered by
specifying the second parameter in the StreamWriter's constructor. If we set it
to true
, it'll append text rather than overwrite it. We add a new
line to an existing file like this:
using (StreamWriter sw = new StreamWriter(@"file.txt", true)) { sw.WriteLine("The appended line"); sw.Flush(); }
Another parameter that may be passed is the encoding, but UTF-8 is default, which should suit us in most cases.
Reading an existing file
The only thing left for us to learn is how to read a file. It isn't any more complicated than writing. As usual, there is a .NET framework class for it - StreamReader. It works pretty much like StreamWriter, but instead of the WriteLine() method we use ReadLine() which returns a line of text from the file and moves to the next line. With this in mind, we'll call it in a while loop. The condition to avoid reading beyond the file may seem a little tricky, however, all we're doing is checking whether the line has been assigned to the variable.
The code to print the file contents to the console would look like this:
using (StreamReader sr = new StreamReader(@"file.txt")) { string s; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } }
The code for our entire application now looks similar to this:
// writing to the file using (StreamWriter sw = new StreamWriter(@"file.txt")) { sw.WriteLine("The first line"); sw.WriteLine("This text is on the second line"); sw.WriteLine("And the third one."); sw.Flush(); } Console.WriteLine("The file has been successfully written."); // appending a text to the file using (StreamWriter sw = new StreamWriter(@"file.txt", true)) { sw.WriteLine("An appended line"); sw.Flush(); } Console.WriteLine("A new line has been successfully appended into the file."); // printing the contents of the file Console.WriteLine("Printing file contents:"); using (StreamReader sr = new StreamReader(@"file.txt")) { string s; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } Console.ReadKey();
The result:
Console application
The file has been successfully written.
A new line has been successfully appended into the file.
Printing file contents:
The first line
This text is on the second line
And the third one.
An appended line
The File class
The .NET framework contains the static File class with pre-made methods that
don't fully replace the StreamReader/StreamWriter classes, but we might get by
without them in most cases. The last example, which prints all of the lines in a
file could be written as follows (don't forget to add
using System.IO
):
string[] lines = File.ReadAllLines(@"file.txt"); foreach (string line in lines) { Console.WriteLine(line); }
The ReadAllLines() method returns all of the lines in a text file as an array of strings. If the file isn't too large, this method is much easier to use. There are other similar methods in the File class such as WriteAllLines(arrayOfStrings, pathToFile) or AppendAllLines(arrayOfStrings, pathToFile). The first one writes lines from a string array into a file, the other appends lines from a string array to an existing file. The class also includes methods that don't work with lines, but with continuous text. These are WriteAllText(text), ReadAllText(), and AppendAllText(text).
In this article, we've omitted exception handling and writing privileges. Next time, we'll look into storing objects into files.
Download
By downloading the following file, you agree to the license termsDownloaded 689x (34.79 kB)