none
How do I append to the Memory Stream RRS feed

  • Question

  •  I have a method that loops through some objects and writes the information to a Memory Stream, I want all this information to be in one memory stream object but i do not see a Append() method....Can some one help me out on getting this done! thanks!
    Give youself a round of applause!!
    Wednesday, June 4, 2008 2:25 PM

Answers

  • If you use mstream.Capacity, you write actual size of stream (not size of data in stream!). Try this:
    // write data to stream  
    MemoryStream stream = new MemoryStream();  
    stream.Write(new byte[] { 1, 2, 3 }, 0, 3);  
    stream.Write(new byte[] { 5, 6, 7 }, 0, 3);  
     
    MemoryStream s2 = new MemoryStream();  
    // and write data from stream to s2  
    s2.Write(stream.GetBuffer(), 0, (int)stream.Length);  
     
    • Marked as answer by Tryin2Bgood Wednesday, June 4, 2008 4:54 PM
    Wednesday, June 4, 2008 4:00 PM

All replies

  • Hi,

    I don't understand, why you can't use a "Write" function?
    Wednesday, June 4, 2008 3:45 PM
  • ok but how would I use it...when I have my MemoryStream

    I do something like mstream.Write(mstream.GetBuffer(), 0, mstream.Capacity)

    but i get an error saying that the internal buffer can not be accessed....so how would I use the Write() method so that it will take the exsiting stream along with another stream...
    Give youself a round of applause!!
    Wednesday, June 4, 2008 3:50 PM
  • If you use mstream.Capacity, you write actual size of stream (not size of data in stream!). Try this:
    // write data to stream  
    MemoryStream stream = new MemoryStream();  
    stream.Write(new byte[] { 1, 2, 3 }, 0, 3);  
    stream.Write(new byte[] { 5, 6, 7 }, 0, 3);  
     
    MemoryStream s2 = new MemoryStream();  
    // and write data from stream to s2  
    s2.Write(stream.GetBuffer(), 0, (int)stream.Length);  
     
    • Marked as answer by Tryin2Bgood Wednesday, June 4, 2008 4:54 PM
    Wednesday, June 4, 2008 4:00 PM
  • did u test that? when I use it i get exception saying that it can not access it internal buffer
    Give youself a round of applause!!
    Wednesday, June 4, 2008 4:42 PM
  •  Yes, I tested it. I used Visual C# 2008 with .Net 2.0 and it work.
    Wednesday, June 4, 2008 4:46 PM
  • great, I got it to work, I am using C# 2005 .Net 2.0, it still does not solve my problem though but that is out of the scope of this post.. thanks!
    Give youself a round of applause!!
    Wednesday, June 4, 2008 4:54 PM