Answers for "c# read large file"

C#
0

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();
    }
}
Posted by: Guest on December-13-2020
0

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);
       //Do something after read. like write to file
    }                           
}
Posted by: Guest on January-23-2021

C# Answers by Framework

Browse Popular Code Answers by Language