Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:00:07+00:00 2026-06-18T15:00:07+00:00

I want to read a file into a RichTextBox without using LoadFile (I might

  • 0

I want to read a file into a RichTextBox without using LoadFile (I might want to display the progress). The file contains only ASCII characters.

I was thinking of reading the file in chunks.

I have done the following (which is working):

const int READ_BUFFER_SIZE = 4 * 1024;

BinaryReader reader = new BinaryReader(File.Open("file.txt", FileMode.Open));

byte[] buf = new byte[READ_BUFFER_SIZE];
do {
    int ret = reader.Read(buf, 0, READ_BUFFER_SIZE);
    if (ret <= 0) {
        break;
    }

    string text = Encoding.ASCII.GetString(buf);
    richTextBox.AppendText(text);
} while (true);

My concern is:

string text = Encoding.ASCII.GetString(buf);

I have seen that it is not possible to add a byte[] to a RichTextBox.

My questions are:

  • Will a new string object be allocated for every chunk which is read?

  • Isn’t there a better way not to have to create a string object just for appending the text to the RichTextBox?

  • Or, is it more efficient to read lines from the file (StreamReader.ReadLine) and just add to the RichTextBox the string returned?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:00:08+00:00Added an answer on June 18, 2026 at 3:00 pm

    Will a new string object be allocated for every chunk which is read?

    Yes.

    Isn’t there a better way not to have to create a string object just for appending the text to the RichTextBox?

    No, AppendText requires a string

    Or, is it more efficient to read lines from the file (StreamReader.ReadLine) and just add to the RichTextBox the string returned?

    No, that’s considerably less efficient. You’ll now create a new string object much more frequently. Which is okay from the garbage collected heap perspective, you don’t create more garbage. But it is absolute murder on the RichTextBox, it constantly needs to re-allocate its own buffer. Which includes moving all the text previously read. What you have is already good, you should just use a much larger READ_BUFFER_SIZE.

    Unfortunately there are conflicting goals here. You don’t want to make the buffer larger than 39,999 bytes or the strings end up in the Large Object Heap and clog it up until a gen# 2 garbage collection happens. But the RTB will be much happier if you go considerably past that size, like a megabyte if the file is so large that you need a progress bar.

    If you want to make it really efficient then you need to replace RichTextBox.LoadFile(). The underlying Windows message is EM_STREAMIN, it uses a callback mechanism to stream in the text. You can technically replace the callback to do what the default one does in RichTextBox, plus update a progress bar. It does permit getting rid of the strings btw. The pinvoke is pretty unfriendly, use the Reference Source for guidance.

    Take the easy route first, increase the buffer size. Only consider using the pinvoke route when your code is considerably slower than using File.ReadAllText().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read a file into an array of characters using fread. Now I want
I want to read a file into an ArrayList of Characters. At first I
I want to read lines from a file into a Set or List. Is
i want simultaneously read and write data into file. Can i use StreamReader and
I'm trying to read values from a text file into a hashtable, I want
I want to read a triangle of integer values from a file into a
I want to read bytes from a wave file into an array. Since the
I want to read a whole file into a string and then use the
I want to read a file into a byte array. So, I am reading
I want to read an entire file into a python list any one knows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.