You want to read a text file using StreamReader from the System.IO namespace in the C# language and base class libraries. By using the 'using' statement, which is ideal for this purpose, you can both perform actual file IO and dispose of the system resources. This report contains several examples on using StreamReader in your C# programs based on the .NET framework.
Here we see that the using statement allows you to leave the file disposal and opening routines to the C# compiler's knowledge of scope. This statement will be compiled to opcodes that instruct the CLR to do all the error-prone and tedious cleanup work with the file handles in Windows.
=== Program that uses StreamReader (C#) ===
using System;
using System.IO;
class Program
{
static void Main()
{
//
// It will free resources on its own.
//
string line;
using (StreamReader reader = new StreamReader("file.txt"))
{
line = reader.ReadLine();
}
Console.WriteLine(line);
}
}
=== Output of the program ===
First line of your file.txt file.Note on the above code. Scope is important here. When objects go out of scope, the garbage collector or finalization code is run and your program's memory is reclaimed. Recall that a scope in your C# code is a block between brackets. Scope allows the compiler to make many assumptions about your program.
Here we look at how you can put an entire file into a collection. One very common requirement for programs is that you need to read in a file line-by-line. You want to store all those lines in a generic List or ArrayList. Here's an example that reads in a file line-by-line and stores it in a List.
=== Program that reads all lines (C#) ===
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main()
{
//
// Read in a file line-by-line, and store it all in a List.
//
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
Console.WriteLine(line); // Write to console.
}
}
}
}
=== Output of the program ===
First line of your file.txt file.
Second line.
Third line.
Last line.Overview of the code example. It creates a List. In the above code, a new List generic object that stores strings is created. The example shows the using keyword. The using keyword surrounds the StreamReader. This ensures correct disposal of resources.
Note on the loop. It runs a while loop. This loop reads in lines until the end of the file. ReadLine returns null at the end of a file—no need to check for EOF. Finally, it adds lines to the List, resulting in a List that stores all the lines in a file.
Here we see an error-prone way of using StreamReader, without the using statement. We open a file in one line, deal with the file in another few lines, and then make a call to close the file. The code shown below does this in the C# language, but is cumbersome and unclear.
=== Program that uses Dispose (C#) ===
using System;
using System.IO;
class Program
{
static void Main()
{
//
// Read a line from a file the old way.
//
StreamReader reader = new StreamReader("file.txt");
string line = reader.ReadLine();
reader.Close();
// You should call Dispose on 'reader' here, too.
reader.Dispose();
Console.WriteLine(line);
}
}
=== Output of the program ===
First line of file.txt file.There are several problems with trying to use the Close and Dispose methods on your own. It is harder to type and read, and bugs could creep into the code. Other than those problems, it works well and efficiently. If the file is not there, it will throw an exception, which you should be on the lookout for. If the except is thrown, you must have a finally block to release the unmanaged resources.
Here we looked ways you can use the StreamReader class for effectively reading in lines of text files. The using keyword has performance that is essentially equal to any manual ways of doing it. Overall, the author feels that the base class library's file IO methods are very efficient and well-designed. The using pattern here can reduce the resources used by your program—both your personal resources, and those of the environment and electrical outlet.