title: How do you remove and add bytes from a byte array in C#
body: <p>I have a configuration file (.cfg) that I am using to create a command line application to add users to a SFTP server application.</p>
  
  <p>The cfg file needs to have a certain number of reserved bytes for each entry in the cfg file.  I am currently just appending a new user to the end of the file by creating a byte array and converting it to a string, then copying it to the file, but i've hit a snag.  The config file requires 4 bytes at the end of the file.</p>
  
  <p>The process I need to accomplish is to remove these trailing bytes from the file, append the new user, then append the bytes to the end.</p>
  
  <p>So, now that you have some context behind my problem.</p>
  
  <p>Here is the question:</p>
  
  <p><strong>How do you remove and add bytes from a byte array?</strong></p>
  
  <p>Here is the code I've got so far, it reads the user from one file and appends it to another.</p>
  
  <pre><code>static void Main(string[] args)
          {
              System.Text.ASCIIEncoding code = new System.Text.ASCIIEncoding();     //Encoding in ascii to pick up mad characters
              StreamReader reader = new StreamReader("one_user.cfg", code, false, 1072);
  
              string input = "";
              input = reader.ReadToEnd();
  
              //convert input string to bytes
              byte[] byteArray = Encoding.ASCII.GetBytes(input);
              MemoryStream stream = new MemoryStream(byteArray);
  
              //Convert Stream to string
              StreamReader byteReader = new StreamReader(stream);
              String output = byteReader.ReadToEnd();
              int len = System.Text.Encoding.ASCII.GetByteCount(output);
  
              using (StreamWriter writer = new StreamWriter("freeFTPdservice.cfg", true, Encoding.ASCII, 5504))
              {
                  writer.Write(output, true);
                  writer.Close();
              }
  
              Console.WriteLine("Appended: " + len);
              Console.ReadLine();
              reader.Close();
              byteReader.Close();
          }
  </code></pre>
  
  <p>To try and illustrate this point, here is a "diagram".  </p>
  
  <blockquote>
    <p>1) Add first user</p>
    
    <p>File(appended text)Bytes at end (zeros)</p>
    
    <p>2) Add second user</p>
    
    <p>File(appended text)(appended text)bytes at end (zeros)</p>
    
    <p>and so on.</p>
  </blockquote>