In C#, File.OpenText() opens an existing text file and returns a StreamReader that decodes UTF-8. You then read with ReadLine, Read, or ReadToEnd.
It is the right choice when you want to handle a file one line at a time, or stop early, without loading the whole file. Wrap it in a using block so the file is closed.
This is a static method of System.IO.File. Call it as File.OpenText(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Open the reader in a using statement. ReadLine returns null when the file ends.
Opens the file for reading and returns a UTF-8 StreamReader.
reader.ReadLine()
Returns the next line without its line break, or null at the end.
reader.ReadToEnd()
Returns everything that is left as one string.
File.OpenText is a short way to write new StreamReader(path). For a small file, ReadAllLines is simpler; for large files File.ReadLines gives a lazy sequence.
Library declarations
C#
public static StreamReader OpenText(string path);
Parameters
Parameter
Description
path
The text file to read. It must exist.
Return Value
Return Value
Description
StreamReader
A reader at the start of the file. Dispose it when you finish.
C# Examples
Example 1 — Read the First Two Lines
Open a small file and call ReadLine twice.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-opentext");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "words.txt");
File.WriteAllLines(path, new[] { "first", "second", "third" });
using (StreamReader reader = File.OpenText(path)) {
Console.WriteLine(reader.ReadLine());
Console.WriteLine(reader.ReadLine());
}
Directory.Delete(root, true);
}
}
Output
first
second
How It Works
Set up three lines.WriteAllLines saves first, second, and third, each followed by a line break.
Each call moves forward. The reader remembers its position, so the first ReadLine returns first and the second returns second.
No need to read it all. The third line is never read. Streams are handy when you only need the top of a big file.
Example 2 — Number Every Line with a while Loop
Keep calling ReadLine until it returns null.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-opentext-loop");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "colors.txt");
File.WriteAllLines(path, new[] { "red", "green", "blue" });
int number = 0;
using (StreamReader reader = File.OpenText(path)) {
string line;
while ((line = reader.ReadLine()) != null) {
number++;
Console.WriteLine(number + ": " + line);
}
}
Directory.Delete(root, true);
}
}
Output
1: red
2: green
3: blue
How It Works
Assign and test in one step.(line = reader.ReadLine()) != null reads a line, stores it in line, and checks that it is not null.
null means the end. After blue there is nothing left, so ReadLine returns null and the loop stops by itself.
Count as you go. The counter goes up before each print, which gives the numbers 1 to 3.
Example 3 — Peek, Read One Character, Then ReadToEnd
Look at the next character without moving, take it, and read the rest.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-opentext-peek");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "hello.txt");
File.WriteAllText(path, "Hello World");
using (StreamReader reader = File.OpenText(path)) {
Console.WriteLine((char)reader.Peek());
reader.Read();
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine(reader.EndOfStream);
}
Directory.Delete(root, true);
}
}
Output
H
ello World
True
How It Works
Peek does not move.Peek returns the next character as a number, 72 for the letter H, without using it up. The (char) cast shows H.
Read moves one step.reader.Read() takes that H and moves forward. Its result is not used here.
ReadToEnd takes the rest. Everything after the first character comes back as ello World.
Nothing is left.EndOfStream is now True, which is a handy loop test for reading in other styles.
Frequently Asked Questions
A StreamReader that reads UTF-8 text from an existing file. Use ReadLine, Read, Peek, or ReadToEnd, and dispose it when you are done.
ReadAllLines loads every line into an array at once. OpenText reads one line at a time, so it uses less memory and can stop early.
ReadLine returns null at the end. You can also check reader.EndOfStream before reading.
No. It throws FileNotFoundException. Use File.CreateText or File.AppendText when you want the file created.
It assumes UTF-8 but detects a byte order mark. For a specific encoding without a BOM, use new StreamReader(path, Encoding.Latin1) or another Encoding.