[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-03-26
2
Medium Priority
?
1,954 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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