Rahul Singh

Rahul Singh

  • 1.4k
  • 209
  • 9.5k

Read a large Text File ( 250 MB) And Write in TextBox C#

Jan 30 2018 12:39 AM
i am Working on a Project on tech asp.net C#
 
I Have a Notepad file almost 250 MB i want to Write its Text on TextBox  
 
I used this Code but it taking too much time at last Throwing error  
 
OutOfMemoryException
 
any one can suggest me what is the Easiest way.
 
Here is the Code Below. 
 
StringBuilder fileText = new StringBuilder();
using (FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
string line = "";
txtReader.Text = "";
while ((line = sr.ReadLine()) != null)
{
//fileText.AppendLine(line);
txtReader.Text += line.ToString();
}
}
txtReader.Text = fileText.ToString();

Answers (7)