I am sure that we are all familiar and probably use a lot of codes presented in books, on the Internet, etc. when reading a file using C #. Something simple ...
StringBuilder fiContents = new StringBuilder(); using (StreamReader fi = new StreamReader(@"C:\a_file.txt")) { while (!fi.EndOfStream) { fiContents.Append(fi.ReadLine); } }
Or maybe something short like ...
using (StreamReader fi = new StreamReader(@"C:\a_file.txt")) fiContents.Append(fi.ReadToEnd());
Now let go of Super Saiyan for a moment and do some really bizarre things like BackgroundWorker that will let us show the boot image (this is what I will use), provide a process countdown timer or ProgressBar .
public void ReadFile(string filename) { BackgroundWorker procFile = new BackgroundWorker(); // Progress 1: If we want to show the progress we need to enable the following property // procFile.WorkerReportsProgress = true; profile.DoWork += new DoWorkEventHandler((object obj, DoWorkEventArgs ev) => { StringBuilder fiContents = new StringBuilder(); using (StreamReader fi = new StreamReader(filename)) { while (!fi.EndOfStream) { // Progress 2: Report the progress, this will be dealt with by the respective handler (below). // procFile.ReportProgress((int)(fi.BaseStream.Length / fi.BaseStream.Position) / 100); fiContents.Append(fi.ReadLine); } } ev.Result = fiContents; } /* Progress 3: The handler below will take care of updating the progress of the file as it processed. procFile.ProgressChanged += new ProgressChangedEventHandler((object obj, ProgressChangedEventArgs ev) => { // Progress 4: Do something with the value, such as update a ProgressBar. // .... } */ procFile.RunWorkerCompleted += new RunWorkerCompletedEventHandler((object obj, RunWorkerCompletedEventArgs ev) => { // Do something with the result (ev.Result), bearing in mind, it is a StringBuilder and the ev.Result is an object. StringBuilder result = ev.Result as StringBuilder; // .... } }
+++++ +++++ +++++ +++++
Time for the actual question ... Above was a warm-up and show the current level of understanding, so I do not consider them as promising answers.
I basically do the last code example above (i.e. using BackgroundWorker ) and dumping the contents of what is read on a RichTextBox . Simple stuff.
The problem I encountered is handling large files (e.g. ~ 222 MB). The fact is that I just took .txt, reading it, clicking on the result built using StringBuilder in RichTextBox . It cannot load the file, I get an OutOfMemoryException . One way, which takes up a significant part (and still does not load the file), iterates over the line and adds each character (like a char ) from the StringBuilder file.
I have always used the simplest and easiest means of reading files (for example, the above examples), but does anyone have any recommendations for improving this? Ways to handle extremely large files? etc.
Even as part of the discussion, I would welcome your ideas.
+++++ +++++ +++++ +++++
Edit 1 (@TaW): An exception was thrown while trying to put string in a RichTextBox ...
FileProcessing.RunWorkerCompleted += new RunWorkerCompletedEventArgs((object obj, RunWorkerCompletedEventArgs e) => { // 'Code' is the RichTextBox in question... Code.Text = ""; if (e.Result is StringBuilder) { Code.Text = (e.Result as StringBuilder).ToString(); } }