Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Memorystream : Invalid Operation Exception

Posted on: 2012-06-13

Memorystream that is initialized with the constructor that take another stream can raise the exception "Invalid Operation Exception".

The proper way to is to use the Write property after using the empty constructor.

 var encoding= new UnicodeEncoding(); 
 Byte[] bytesToCompress = encoding.GetBytes("Test123); 
 var streamToCompress = new MemoryStream(); 
 streamToCompress.Write(bytesToCompress, 0, bytesToCompress.Length); 

You can use Unicode encoding but also UTF8 or any other file encoding available.