Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a BinaryWriter closes the outer Stream on disposal, and how to prevent that? (.NET C#)

I have one method that receives a Stream to write on it using a BinaryWriter. But when I dispose this BinaryWriter it also closes the stream. Can I leave it undisposed so I can leave my stream open?

like image 603
Jader Dias Avatar asked Jul 05 '09 20:07

Jader Dias


1 Answers

As of .NET 4.5, the BinaryWriter class has a new constructor that takes a boolean parameter to indicate whether to leave the stream open or not.

Ref: http://msdn.microsoft.com/en-us/library/gg712841.aspx

public BinaryWriter(     Stream output,     Encoding encoding,     bool leaveOpen ) 
like image 169
Dave Van den Eynde Avatar answered Sep 24 '22 16:09

Dave Van den Eynde