Solved

Reading and writig a large file in C# takes too long

Posted on 2007-03-26
2
1,950 Views
Last Modified: 2013-12-17
Hello experts:
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
    openFileDialog1.ShowDialog();
   string MyFile = openFileDialog1.FileName;

//select output file
   SaveFileDialog1.ShowDialog();
   string OutFile = SaveFileDialog1.FileName;

// read records into Array
   StreamReader objReader = new StreamReader(MyFile);
   string sLine="";
   string Newline="";
   ArrayList arrText = new ArrayList();
   while (sLine != null)
   {
  sLine = objReader.ReadLine();
  if (sLine != null)
    arrText.Add(sLine);
   }
  objReader.Close();

//write records            
                               
  StreamWriter streamOut = new StreamWriter(OutFile);

  foreach (string sOutput in arrText)
  Newline = sOutput.Replace(OldValue, NewValue);      
  streamOut.WriteLine(Newline);

  streamOut.Close();      
  Application.Exit();                              
}

Any help or suggestion will be greatly appreciated.

Thanks

lancerxe
0
Comment
Question by:lancerxe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18796719
Instead of keeping the WHOLE thing in memory at once...

Open two files...source and temp target.
Read a line from source file and echo it to temp target with the changes.
When done, close both files.
Delete source.
Rename temp target.
Done.
0
 

Author Comment

by:lancerxe
ID: 18800325
Thanks Idle Mind
That did it.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question