title: C# Trying to replace a byte while using MemoryStream class
body: <p>I get a text file from a mainframe and sometimes there are some 0x0D injected into the middle of the text lines.</p>
  
  <p>The previos programmer created a method using the FileStream class. This method works fine but is taking around 30 minutes to go thru the entire file.</p>
  
  <p>My thought was to pass the text lines that are needed (about 25 lines) to a method to decrease the processing time.</p>
  
  <p>I've been working with the MemoryStream class but am having issue where it does not find the <code>0x0D</code> control code.</p>
  
  <p>Here is the current FileStream method:</p>
  
  <pre><code>private void ReplaceFileStream(string strInputFile)
  {
      FileStream fileStream = new FileStream(strInputFile, FileMode.Open, FileAccess.ReadWrite);
      byte filebyte;
  
      while (fileStream.Position &lt; fileStream.Length)
      {
          filebyte = (byte)fileStream.ReadByte();
          if (filebyte == 0x0D)
          {
              filebyte = 0x20;
              fileStream.Position = fileStream.Position - 1;
              fileStream.WriteByte(filebyte);
          }
      }
      fileStream.Close();
  }
  </code></pre>
  
  <p>and here is the MemoryStream method:</p>
  
  <pre><code>private void ReplaceMemoryStream(string strInputLine)
  {
      byte[] byteArray = Encoding.ASCII.GetBytes(strInputLine);
      MemoryStream fileStream = new MemoryStream(byteArray);
  
      byte filebyte;
  
      while (fileStream.Position &lt; fileStream.Length)
      {
          filebyte = (byte)fileStream.ReadByte();
          if (filebyte == 0x0D)
          {
              filebyte = 0x20;
              fileStream.Position = fileStream.Position - 1;
              fileStream.WriteByte(filebyte);
          }
      }
      fileStream.Close();
  }
  </code></pre>
  
  <p>As I have not used the MemoryStream class before am not that familar with it. Any tips or ideas?</p>