Table of Contents

Class StreamWriter

Namespace
System.IO
Assembly
System.IO.Streams.dll

Implements a TextWriter for writing characters to a stream in a particular encoding.

public class StreamWriter : TextWriter, IDisposable
Inheritance
StreamWriter
Implements
Inherited Members
Extension Methods

Constructors

StreamWriter(Stream)

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

public StreamWriter(Stream stream)

Parameters

stream Stream

The stream to write to.

Exceptions

ArgumentNullException

stream is null.

ArgumentException

stream is not writable.

Properties

BaseStream

Gets the underlying stream that interfaces with a backing store.

public virtual Stream BaseStream { get; }

Property Value

Stream

The stream this StreamWriter is writing to.

Encoding

Gets the Encoding in which the output is written.

public override Encoding Encoding { get; }

Property Value

Encoding

The Encoding specified in the constructor for the current instance, or UTF8Encoding if an encoding was not specified.

NewLine

Gets or sets the line terminator string used by the current TextWriter.

public override string NewLine { get; set; }

Property Value

string

Methods

Close()

Closes the current StreamWriter object and the underlying stream.

public override void Close()

Remarks

This method overrides Close(). This implementation of Close() calls the Dispose(bool) method passing a true value. You must call Close() to ensure that all data is correctly written out to the underlying stream.Following a call to Close(), any operations on the StreamWriter might raise exceptions. If there is insufficient space on the disk, calling Close() will raise an exception. Flushing the stream will not flush its underlying encoder unless you explicitly call Flush() or Close().

Dispose(bool)

Causes any buffered data to be written to the underlying stream, releases the unmanaged resources used by the StreamWriter, and optionally the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

When the disposing parameter is true, this method releases all resources held by any managed objects that this StreamWriter references. This method invokes the Dispose(bool) method of each referenced object.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

public override void Flush()

Remarks

This method overrides TextWriter.Flush. Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close().

Exceptions

ObjectDisposedException

The current writer is closed.

IOException

An I/O error has occurred.

Write(char)

Writes a character to the stream.

public override void Write(char value)

Parameters

value char

Remarks

This method overrides Write(char). The specified character is written to the underlying stream unless the end of the stream is reached prematurely.

Write(string)

Writes a string to the text stream.

public override void Write(string value)

Parameters

value string

The string to write.

WriteLine()

Writes a line terminator to the text stream.

public override void WriteLine()

WriteLine(string)

Writes a string to the stream, followed by a line terminator.

public override void WriteLine(string value)

Parameters

value string

Remarks

This overload is equivalent to the Write(string) overload. The line terminator is defined by the CoreNewLine field. This method does not search the specified string for individual newline characters(hexadecimal 0x000a) and replace them with NewLine.