Extended file reading

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(); } } 
+6
source share
3 answers

Do you have a limitation that requires using a RichTextBox as a control to display your content? This control is not virtualized and will cause performance problems (and the appearance of a memory error).

There is a family of document view controls that are best suited for displaying large documents. Various controls exist depending on your needs (fixed, flowing through the page or scrolling). In addition, you get search, printing, scaling and several other functions that are often useful for viewing large documents.

+2
source

Have you tried MemoryMapped ,
Its a pretty useful library for processing large files

0
source

it’s not about advanced reading, but how to use performance limitations (Winforms). Perhaps you can make it work in WPF, but in Winforms neither RichTextBox nor TextBox can contain such a large number of lines / text.

I advise you to redo this to present data to users in small pieces. It's not that they would like to scroll over 100,000 lines. Processing them in memory is not a problem; 200 MB are small here; you can, for example, easily find in it in memory, etc.

0
source

All Articles