Reading and writig a large file in C# takes too long
Posted on 2007-03-26
I have a huge file (634 MB) that I want to read and then write, after changing the value of 3 fields.
The code is below. After the application ran for 2 hours(it was in the read records into Array section) I killed it.
private void btnChange_Click(object sender, System.EventArgs e)
string OldValue = txtOldValue.Text;
string NewValue = txtNewValue.Text;
//Select input file
string MyFile = openFileDialog1.FileName;
//select output file
string OutFile = SaveFileDialog1.FileName;
// read records into Array
StreamReader objReader = new StreamReader(MyFile);
ArrayList arrText = new ArrayList();
while (sLine != null)
sLine = objReader.ReadLine();
if (sLine != null)
StreamWriter streamOut = new StreamWriter(OutFile);
foreach (string sOutput in arrText)
Newline = sOutput.Replace(OldValue, NewValue);
Any help or suggestion will be greatly appreciated.