How to Read Large File in C#

In today's article I will show you how you can read the content of a very larger file of size 2GB and more using c#.net. In this simple tutorial I will show you with an example for reading and display the content of a text file in c#.net. Here we will use a console application. 

As all we know there are three most efficient ways to read the content of a file in C#.Net. They as follow:

Here in our tutorial, we will use StreamReader to read the text file because we are going to use a text file almost of 1GB in size.
Text File of Large size (almost 1GB)
Please check the below given code. 

 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(); 
Here in above code, I have taken the path of the text file in a string variable, and I have used File.Exists method to validate first whether the file exists or not. If file does not exist on that case, it will display an error message to user.

Now I have created object of the StreamReader to read the file in memory stream. Now above as I mention StreamReader is the most efficient way to read the large text file, in next line we are reading each line of the text file at run time and displaying it. You can collect as per you convince. Now let's place a break point and check the output.

Large Text File Content
Note 1. The above output taken me only less then 1 sec to get the result.
2. To make you sample text file open the notepad and copy past the content of the file again and again until you get the desired size of the text file.

2 comments

  1. aj.com
    aj.com
    One question asked by interviewer that one file is taking too much time to upload on server what we can do so that it can upload quick.
  2. Vinay Singh
    Vinay Singh
    Hi AJ, as per my understanding is a file taking much time to upload you can do below mentioned operation while uploading the file.
    Few Checkes:
    ----------------------
    1. Check for bandwidth.
    2. Reduce the size of your files. Large files will take longer to upload than smaller files. If possible, try to compress your files or break them up into smaller chunks.
    Code Level changes:
    -------------------------------
    1. If you are using Asp.net core never use byte[] or MemoryStream. Always use StreamContent. Using Stream content avoid the loading of all the file in the memory.
    2. In your upload you can increase the upload file size limit by putting attribute like
    [RequestSizeLimit(10L * 1024L * 1024L * 1024L)]
    [RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024L * 1024L * 1024L)].