title: How to remove data from a MemoryStream
body: <p>I cannot get this to work. I have a MemoryStream object. This class
  has a Position property that tells you how many bytes you have read.</p>
  
  <p>What I want to do is to delete all the bytes between 0 and Position-1</p>
  
  <p>I tried this:</p>
  
  <pre><code>MemoryStream ms = ...
  ms.SetLength(ms.Length - ms.Position);
  </code></pre>
  
  <p>but at some point my data gets corrupted.</p>
  
  <p>So I ended up doing this</p>
  
  <pre><code>MemoryStream ms = ...
  byte[] rest = new byte[ms.Length - ms.Position];
  ms.Read(rest, 0, (int)(ms.Length - ms.Position));
  ms.Dispose();
  ms = new MemoryStream();
  ms.Write(rest, 0, rest.Length);
  </code></pre>
  
  <p>which works but is not really efficient.</p>
  
  <p>Any ideas how I can get this to work?</p>
  
  <p>Thanks</p>