c# read big file line by line c# code example
Example 1: c# read huge file
class Program
{
static void Main(String[] args)
{
const int bufferSize = 1024;
var sb = new StringBuilder();
var buffer = new Char[bufferSize];
var length = 0L;
var totalRead = 0L;
var count = bufferSize;
using (var sr = new StreamReader(@"C:\Temp\file.txt"))
{
length = sr.BaseStream.Length;
while (count > 0)
{
count = sr.Read(buffer, 0, bufferSize);
sb.Append(buffer, 0, count);
totalRead += count;
}
}
Console.ReadKey();
}
}
Example 2: c# read large file
int filesize = (new FileInfo(filePath)).Length;
using (Stream sr = File.OpenRead(filePath))
{
int maxchunk = 1024;
for(long x = 0; x < totalChunk; x++ )
{
long LefttotalLegth = filesize - (x * maxchunk);
long leghtcopy = Math.Min(maxchunk, LefttotalLegth);
byte[] chunkbyte = new byte[leghtcopy];
sr.Read(chunkbyte, 0, chunkbyte.Length);
}
}