// Read entire text file content in one string
string text = File.ReadAllText(textFile);
Console.WriteLine(text);
The File.ReadAllLines() method opens a text file, reads all lines of the file into a string array, and then closes the file. The following code snippet reads a text file into an array of strings.
// Read a text file line by line.
string[] lines = File.ReadAllLines(textFile);
foreach (string line in lines)
Console.WriteLine(line);
One more way to read a text file is using a StreamReader class that implements a TextReader and reads characters from a byte stream in a particular encoding. The ReadLine method of StreamReader reads one line at a time.
// Read file using StreamReader. Reads file line by line
using(StreamReader file = new StreamReader(textFile)) {
int counter = 0;
string ln;
while ((ln = file.ReadLine()) != null) {
Console.WriteLine(ln);
counter++;
}
file.Close();
Console.WriteLine($ "File has {counter} lines.");
}
The complete code sample uses the above-discussed methods to read a text file and display its content to the console. To test this code, find a text file (or create one with some text in it) on your machine and change the "textFile" variable to the full path of your .txt file.
using System;
using System.IO;
namespace ReadATextFile {
class Program {
// Default folder
static readonly string rootFolder = @ "C:\Temp\Data\";
//Default file. MAKE SURE TO CHANGE THIS LOCATION AND FILE PATH TO YOUR FILE
static readonly string textFile = @ "C:\Temp\Data\Authors.txt";
static void Main(string[] args) {
if (File.Exists(textFile)) {
// Read entire text file content in one string
string text = File.ReadAllText(textFile);
Console.WriteLine(text);
}
if (File.Exists(textFile)) {
// Read a text file line by line.
string[] lines = File.ReadAllLines(textFile);
foreach(string line in lines)
Console.WriteLine(line);
}
if (File.Exists(textFile)) {
// Read file using StreamReader. Reads file line by line
using(StreamReader file = new StreamReader(textFile)) {
int counter = 0;
string ln;
while ((ln = file.ReadLine()) != null) {
Console.WriteLine(ln);
counter++;
}
file.Close();
Console.WriteLine($ "File has {counter} lines.");
}
}
Console.ReadKey();
}
}
}
Summary
The code example in this article taught you how to use the File class to read a text file in C#.
Here is a detailed article on the File class in C#: Working With File Class In C# (c-sharpcorner.com)

Jogn DavidPosted Jan 8, 2021, 10:36 PM
I have noticed that you are using File.Close() when using StreamReader. My question is do you need to do this? Microsoft's examples quite specifically states that when you use StreamReader in the way you are using (see code snippet number 3 above) there is no need as the scope takes care of it.
Faizan HazmaPosted Mar 13, 2020, 9:56 AM
Great Well explained .
JUAN MEJIAPosted Dec 24, 2019, 11:57 PM
Well explained and clean code example.
shreyas adikumarPosted Oct 18, 2019, 12:16 AM
Sir when I am reading into a local text file I am getting permission denied to the folder after publishing the program in the server. Even after giving permission to the folder I am still facing the same problem to read into local desktop
Riley BOW-TPosted Jul 12, 2019, 2:08 AM
Using a stream reader to read a text-file (.txt) data, then save it to the database. Is there a way to check if the same data exist in the database while a user is trying to read/save same text-file??
Varad SarvePosted May 3, 2019, 5:41 AM
" ! " in If condition is wrong
Pankaj kumarPosted Mar 6, 2019, 6:58 PM
How to read .cs file in C#?
varshaPosted Nov 13, 2017, 5:21 AM
I am unable to read the text file in windows form . i am getting the following error An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll Additional information: The given path's format is not supported. I had written like this: String^ strfilename = openFileDialog1->InitialDirectory + openFileDialog1 + filename; String^ readfile = File::ReadAllText(strfilename); can anyone tell me what I could do
K P Singh ChundawatPosted Dec 29, 2014, 7:17 AM
Nice...
Caleb AdeyemoPosted May 4, 2011, 12:47 PM
This is a very helpful article. Please can you explain supposing the file is unsorted and after reading the text file you now want to sort the data it contains, display it to a rich text box and write it to a new file?
Karthiga MPosted Mar 18, 2011, 2:01 AM
I am big fan of your codes. Its very much clear and understandable.Thanks a lot. I have a question to ask you.How to read a text file work by word.I want to update a string in my text file.Can you please help me.
dumb geekPosted Dec 1, 2010, 12:22 PM
would this work? string fileName = @"www.domain.com/folder/file.txt"; ?