This is a common problem with MemoryStream usage in code ported from java.
Try this workaround (it works fine for me):
http://connect.microsoft.com/VisualStudio/feedback/details/164680/streamwriter-incorrectly-disposes-underlying-streamBasically decorate the underlying memory stream (the argument to StreamWriter) as:
streamWriter(new NonDisposingStreamDecorator(memoryStream))
Alex