title: How do I use File.ReadAllBytes In chunks
body: <p>I am using this code </p>
  
  <pre><code>        string location1 = textBox2.Text;
          byte[] bytes = File.ReadAllBytes(location1);
          string text = (Convert.ToBase64String(bytes));
          richTextBox1.Text = text;
  </code></pre>
  
  <p>But when I use a file that is too big I get out of memory exception.</p>
  
  <p>I want to use <code>File.ReadAllBytes</code> in chunks. I have seen code like this below</p>
  
  <pre><code>System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open);
  byte[] buf = new byte[BUF_SIZE];
  int bytesRead;
  
  // Read the file one kilobyte at a time.
  do
  {
      bytesRead = fs.Read(buf, 0, BUF_SIZE);               
      // 'buf' contains the last 1024 bytes read of the file.
  
  } while (bytesRead == BUF_SIZE);
  
  fs.Close();
  
  }
  </code></pre>
  
  <p>But I don't know how to actually convert the <code>bytesRead</code> into a byte array which I will convert to text.</p>
  
  <p>EDIT: Answer found. Here is the code!</p>
  
  <pre><code> private void button1_Click(object sender, EventArgs e)
      {
          const int MAX_BUFFER = 2048;
          byte[] Buffer = new byte[MAX_BUFFER];
          int BytesRead;
          using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
              while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
              {
                  string text = (Convert.ToBase64String(Buffer));
                  textBox1.Text = text;
  
              }
  
  }
  </code></pre>
  
  <p>To change the readable bytes which are in text format, create a new byte and make it equal
  (Convert.FromBase64String(Text)). Thanks everyone!</p>