Windows Dev Center

StreamWriter Constructor (Stream, Encoding, Int32, Boolean)

Initializes a new instance of the StreamWriter class for the specified stream, using the specified encoding, buffer size, and a value that specifies whether to leave the stream open.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public Sub New ( _
	stream As Stream, _
	encoding As Encoding, _
	bufferSize As Integer, _
	leaveOpen As Boolean _


Type: System.IO.Stream
The stream to write to.
Type: System.Text.Encoding
The character encoding to use.
Type: System.Int32
The buffer size.
Type: System.Boolean
true to leave the stream open; otherwise, false.


stream or encoding is Nothing.


bufferSize is negative.


stream is not writable.

This constructor initializes the Encoding property using the encoding parameter, and the BaseStream property using the stream parameter. For additional information, see Encoding.

When leaveOpen is true, the stream is left open after the StreamReader object is disposed. Otherwise, the StreamWriter object calls Dispose on the provided Stream object when StreamWriter.Dispose is called.

Caution noteCaution:

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

Version Notes

Windows Phone

 If you pass an invalid input path to a StreamWriter constructor, the constructor throws InvalidArgumentException instead of DirectoryNotFoundException.

Windows Phone OS

Supported in: 8.1, 8.0

© 2015 Microsoft