How to Read a Text File in C#
The purpose of reading a text file in C# is to retrieve information from the file and process it within a C# program. Text files are commonly used to store data that needs to be read and processed by a program, such as configuration files, log files, or data files, here are the two ways to read a text file in C#:
Through File.ReadAllText
This function reads the entire text file at once and returns it as a string, here is an example of how to use this function to read text file in C#:
using System.IO;
namespace ReadingTextFile
{
class Program
{
static void Main(string[] args)
{
// Give the path of the text file
string filePath = @"C:\Users\aaliy\OneDrive\Desktop\test file.txt";
// Reading text file
string fileContent = File.ReadAllText(filePath);
// Display the content of the text file
Console.WriteLine(fileContent);
}
}
}
Here is the explanation for the example code we have given above:
1: using System; and using System.IO: These lines include the necessary namespaces to use the File.ReadAllText() function. The System namespace contains the Console class that is used to display the content of the text file, and the System.IO namespace contains the File class that reads the text file.
2: namespace ReadingTextFile: The namespace declaration defines a scope for the program and helps to organize the code.
3: class Program: The class declaration defines a blueprint for an object and contains the data and behavior of the object.
4: static void Main(string[] args): The Main method is defined as a static method, which means it can be called without creating a class. The Main method is declared as void because it does not return a value. The string[] args parameter is a string array that can be passed to the program when it runs.
5: string filePath = @”C: :\Users\aaliy\OneDrive\Desktop\test file.txt”: This line declares a string variable file path and assigns the file path of the text file to it. The @ symbol before the file path allows the use of escape characters in the string.
6: string fileContent = File.ReadAllText(filePath): This line uses the File.ReadAllText method to read the entire text file and store the content of the text file in the fileContent string variable. The filePath variable is passed as a parameter to the function to specify the location of the text file.
7: Console.WriteLine(fileContent): This line uses the Console.WriteLine() function which displays the content of the text file on the console. The fileContent variable is passed as a parameter to the Console.WriteLine method to specify the content to be displayed.
Output
The below image displays the output which contains the content written in the text file:
Through StreamReader Class
The StreamReader class is part of the System.IO namespace and is used for reading text files in C#. The StreamReader class reads a text file one line at a time, and it reads a text file from the beginning to the end, here is an example code for it:
using System.IO;
namespace ReadingTextFile
{
class Program
{
static void Main(string[] args)
{
// Specify the path of the text file
string filePath = @"C:\Users\aaliy\OneDrive\Desktop\test file.txt";
// Open the text file
using (StreamReader streamReader = new StreamReader(filePath))
{
// Read the entire text file
while (!streamReader.EndOfStream)
{
string line = streamReader.ReadLine();
// Display each line of the text file
Console.WriteLine(line);
}
}
}
}
}
Here is the explanation for the key differences in the code we used in the previous method:
1: using System; and using System.IO: These lines include the necessary namespaces to use the StreamReader class. The System namespace contains the Console class that is used to display the content of the text file, and the System.IO namespace contains the StreamReader class to read the text file.
2: using (StreamReader streamReader = new StreamReader(filePath)): This line opens the text file using the StreamReader class. The using statement automatically disposes of the StreamReader object after the block of code within the using statement has been executed. The filePath variable is passed as a parameter to the StreamReader constructor to specify the location of the text file.
3: while (!streamReader.EndOfStream): This line creates a while loop that continues to execute as long as the EndOfStream property of the StreamReader object is false. The EndOfStream property returns true when the end of the text file has been reached.
4: string line = streamReader.ReadLine(): This line uses the StreamReader class to read a line of the text file and store the line in the line string variable.
Output
The below image displays the output which contains the content written in the text file:
Conclusion
Reading a text file in C# is a simple task that can be accomplished using either the StreamReader class or the File.ReadAllText method. Choose the method that best fits your needs as both are described with an example.