Search code examples
c#streamreader

StreamReader very slow for big files


I am wanting to read in a file which in this case is 3mb doing this takes around 50-60 seconds, which seems very slow. Does anyone know how to make this faster?

string text = null;
using (StreamReader sr = new StreamReader(file, Encoding.Default))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        text += (line);
        backgroundWorker1.ReportProgress(text.Length);
    }
}

I also need to use a background worker so I can report the percentage that has been loaded (for files around 500mb to 1gb)


Solution

  • Use a StringBuilder to create your line - it's much more performant than string concatenation.

    using System.Text;
    
    //...
    
    StringBuilder text = new StringBuilder();
    using (StreamReader sr = new StreamReader(file, Encoding.Default))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            text.Append(line);
            backgroundWorker1.ReportProgress(text.Length);
        }
    }
    
    // ...
    // Do something with the file you have read in.
    Console.WriteLine(text.ToString());