How to speed up reading from a file? (ReadAllText funcionality)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to create a notepad-type appropriate text editor using CF.

I tried to open a file and read its content to a textbox using StreamReader:

try
{
using (StreamReader sr = File.OpenText(filename))
{
string line;

while ((line = sr.ReadLine()) != null)
{
text += line;
}

sr.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}

theTextBox.Text = text;

Reading a 162kB long text file needs more then one minute. It is too slow.
ReadAllText method is not supported in CF (and not supported in OpenNetCF).

Is there a way to speed up the code snippet above?

Please help!

Frankie
 
How about using the ReadToEnd() method on the StreamReader object?

The reason what you're doing is slow, might be that you're creating a
new string object for each line of the file.
 
Using a StringBuilder, not a string for your text variable would probably
change the results by an order of magnitude or so.

-Chris
 
Thank you very much. It is perfect!

Frankie

Jan Obrestad said:
How about using the ReadToEnd() method on the StreamReader object?

The reason what you're doing is slow, might be that you're creating a
new string object for each line of the file.
 
Back
Top