What are the Different Ways to Read the Contents of File in .NET Core?

The different ways to read the text file content are File.ReadAllText, File.ReadAllLines, StreamReader in .net core using c#.net with example.
In today's article I will show you a simple tutorial with an example for what are the different ways to read the content of a file in .NET core using c#.net. Here we will use console application to read and display the content of a test file. Here I will show you three ways to read file are File.ReadAllText, File.ReadAllLines, StreamReader. So, for this article first we will create a new .NET core console application. Now we will understand all the three ways. Before starting we will create a text file in a location.

Example Text file

Now let's check the content of the text file. 

Text File Content Example

Using File.ReadAllText

File.ReadAllText is used to read all the content of the text file in a single string format. File.ReadAllText  is effective for reading or getting the content of the small size text file. Now let's understand this with an example. 
 Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
    string fileData = File.ReadAllText(filePath);
    Console.WriteLine(fileData);
}
else
{
    Console.WriteLine("sorry file not found...");
}
Console.ReadLine(); 
In above code I have first stored the file path in a string variable named as filepath. To validate file path, exist or not I have used File.Exists(filePath). If file does not exist on that case we will display an error message to user. On other hand we will use File.ReadAllText(path) to read the file content in a string variable. After reading the file content I have used Console.WriteLine to display the content of the text file. Now let's run the code to check the output.

C# code to read text file

press F5 to check the final output.

File.ReadAllText output

Using File.ReadAllLines

File.ReadAllLines is one of the methods which is used to read the text content line by line in the form an array. This methos will help us to read and process each line of code separately.  Now let's understand this with an example written in c#.net.
 Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
    string[] fileData = File.ReadAllLines(filePath);
    foreach (string item in fileData)
    {
        Console.WriteLine(item);
    }
}
else
{
    Console.WriteLine("sorry file not found...");
}
Console.ReadLine(); 
In above code I have taken the path of the file and validated file exists or not, if it exists on that case I have taken a variable of string type and used File.ReadAllLines to read the text file each line in c#.net. After that I have used for each to apply loop to display each line of the text file. Let's run the code to check the output.
File.ReadAllLine Code

Here in above code, we are able to get the text file each line. Now press F5 to check the output.

Text file content

Using StreamReader

StreamReader is used to read the text file holding the very large amount of data. This is the most efficient way to read the large size text file contact in c#. StreamReader does not read all the contact of the file at a time it read line by line in the memory. Now let's try to understand it with an example. 
 Console.WriteLine("******READ TEXT FILE******");
string filePath = "C:\\Personal\\Codemantra99.com\\Articles\\TesttextFile.txt";
if (File.Exists(filePath))
{
    StreamReader strreader = new StreamReader(filePath);
    string linetext;
    while ((linetext = strreader.ReadLine()) != null)
    {
        Console.WriteLine(linetext);
    }
}
else
{
    Console.WriteLine("sorry file not found...");
}
Console.ReadLine(); 
In above code I have taken the path of the file and validated the file exists or not. If file exists on that case, I have created object of the StreamReader by passing the file path. In next like I have declared a string variable and used while loop to read each line of the text file. Now let's run the code to check the output.

StreamReader to read text file

Post a Comment