Export (0) Print
Expand All

StreamWriter Constructor (Stream)

Initializes a new instance of the StreamWriter class for the specified stream, using UTF-8 encoding and the default buffer size.

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

'Declaration
Public Sub New ( _
	stream As Stream _
)

Parameters

stream
Type: System.IO.Stream
The stream to write to.

ExceptionCondition
ArgumentException

stream is not writable.

ArgumentNullException

stream is Nothing.

This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark (BOM), so its GetPreamble method returns an empty byte array. The BaseStream property is initialized using the stream parameter. The position of the stream is not reset.

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.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example demonstrates the StreamWriter constructors.


Public Sub CreateTextFile(ByVal FileName As String, _
    ByVal TextToAdd As String)

    Dim Fs As FileStream = New FileStream(FileName, _
        FileMode.CreateNew, FileAccess.Write, FileShare.None)

    Dim SwFromFile As StreamWriter = New StreamWriter(FileName)
    SwFromFile.Write(TextToAdd)
    SwFromFile.Flush()
    SwFromFile.Close()

    Dim SwFromFileStream As StreamWriter = New StreamWriter(Fs)
    SwFromFileStream.Write(TextToAdd)
    SwFromFileStream.Flush()
    SwFromFileStream.Close()

    Dim SwFromFileStreamDefaultEnc As StreamWriter = _
     New StreamWriter(Fs, System.Text.Encoding.Default)
    SwFromFileStreamDefaultEnc.Write(TextToAdd)
    SwFromFileStreamDefaultEnc.Flush()
    SwFromFileStreamDefaultEnc.Close()

    Dim SwFromFileTrue As StreamWriter = _
     New StreamWriter(FileName, True)
    SwFromFileTrue.Write(TextToAdd)
    SwFromFileTrue.Flush()
    SwFromFileTrue.Close()

    Dim SwFromFileTrueUTF8Buffer = _
     New StreamWriter(FileName, _
     True, System.Text.Encoding.UTF8, 512)
    SwFromFileTrueUTF8Buffer.Write(TextToAdd)
    SwFromFileTrueUTF8Buffer.Flush()
    SwFromFileTrueUTF8Buffer.Close()

    Dim SwFromFileTrueUTF8 = _
     New StreamWriter(FileName, True, _
     System.Text.Encoding.UTF8)
    SwFromFileTrueUTF8.Write(TextToAdd)
    SwFromFileTrueUTF8.Flush()
    SwFromFileTrueUTF8.Close()

    Dim SwFromFileStreamUTF8Buffer = _
     New StreamWriter(Fs, System.Text.Encoding.UTF8, 512)
    SwFromFileStreamUTF8Buffer.Write(textToAdd)
    SwFromFileStreamUTF8Buffer.Flush()
    SwFromFileStreamUTF8Buffer.Close()

End Sub


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft