StreamWriter Class

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

Syntax

Declaration

[Serializable]
[ComVisible(true)]
public class StreamWriter : TextWriter, IDisposable

Inheritance Hierarchy

Constructors summary

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

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

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

Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

Fields summary

Provides a StreamWriter with no backing store that can be written to, but not read from.

Properties summary

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to Write(Char).

Gets the underlying stream that interfaces with a backing store.

Gets the Encoding in which the output is written.

Methods summary

Releases the unmanaged resources used by the StreamWriter and optionally releases the managed resources.

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

Clears all buffers for this stream asynchronously and causes any buffered data to be written to the underlying device.

Writes a character to the stream.

Writes a character array to the stream.

Writes a subarray of characters to the stream.

Writes a string to the stream.

Writes a character to the stream asynchronously.

Writes a subarray of characters to the stream asynchronously.

Writes a string to the stream asynchronously.

Writes a line terminator asynchronously to the stream.

Writes a character followed by a line terminator asynchronously to the stream.

Writes a subarray of characters followed by a line terminator asynchronously to the stream.

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

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

      The stream to write to.

    Exceptions

  • StreamWriter(Stream, Encoding)

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

    public StreamWriter(Stream stream, Encoding encoding)

    Parameters

    • stream

      The stream to write to.

    • encoding

      The character encoding to use.

    Exceptions

  • StreamWriter(Stream, Encoding, Int32)

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

    public StreamWriter(Stream stream, Encoding encoding, int bufferSize)

    Parameters

    • stream

      The stream to write to.

    • encoding

      The character encoding to use.

    • bufferSize

      The buffer size, in bytes.

    Exceptions

  • StreamWriter(Stream, Encoding, Int32, Boolean)

    Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size, and optionally leaves the stream open.

    public StreamWriter(Stream stream, Encoding encoding, int bufferSize, bool leaveOpen)

    Parameters

    • stream

      The stream to write to.

    • encoding

      The character encoding to use.

    • bufferSize

      The buffer size, in bytes.

    • leaveOpen

      true to leave the stream open after the StreamWriter object is disposed; otherwise, false.

    Exceptions

Fields

  • Null

    Provides a StreamWriter with no backing store that can be written to, but not read from.

    public static readonly StreamWriter Null

Properties

  • AutoFlush

    Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to Write(Char).

    public virtual bool AutoFlush { get; set; }

    Property Value

  • BaseStream

    Gets the underlying stream that interfaces with a backing store.

    public virtual Stream BaseStream { get; }

    Property Value

    • The stream this StreamWriter is writing to.

  • Encoding

    Gets the Encoding in which the output is written.

    public override Encoding Encoding { get; }

    Property Value

    Overrides

Methods

  • Dispose(Boolean)

    Releases the unmanaged resources used by the StreamWriter and optionally releases the managed resources.

    protected override void Dispose(bool disposing)

    Parameters

    • disposing

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

    Exceptions

    Overrides

  • Flush()

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

    public override void Flush()

    Exceptions

    Overrides

  • FlushAsync()

    Clears all buffers for this stream asynchronously and causes any buffered data to be written to the underlying device.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task FlushAsync()

    Returns

    • A task that represents the asynchronous flush operation.

    Exceptions

    Overrides

  • Write(Char)

    Writes a character to the stream.

    public override void Write(char value)

    Parameters

    • value

      The character to write to the stream.

    Exceptions

    Overrides

  • Write(Char[])

    Writes a character array to the stream.

    public override void Write(char[] buffer)

    Parameters

    • buffer

      A character array containing the data to write. If buffer is null, nothing is written.

    Exceptions

    Overrides

  • Write(Char[], Int32, Int32)

    Writes a subarray of characters to the stream.

    public override void Write(char[] buffer, int index, int count)

    Parameters

    • buffer

      A character array that contains the data to write.

    • index

      The character position in the buffer at which to start reading data.

    • count

      The maximum number of characters to write.

    Exceptions

    Overrides

  • Write(String)

    Writes a string to the stream.

    public override void Write(string value)

    Parameters

    • value

      The string to write to the stream. If value is null, nothing is written.

    Exceptions

    Overrides

  • WriteAsync(Char)

    Writes a character to the stream asynchronously.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteAsync(char value)

    Parameters

    • value

      The character to write to the stream.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteAsync(Char[], Int32, Int32)

    Writes a subarray of characters to the stream asynchronously.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteAsync(char[] buffer, int index, int count)

    Parameters

    • buffer

      A character array that contains the data to write.

    • index

      The character position in the buffer at which to begin reading data.

    • count

      The maximum number of characters to write.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteAsync(String)

    Writes a string to the stream asynchronously.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteAsync(string value)

    Parameters

    • value

      The string to write to the stream. If value is null, nothing is written.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteLineAsync()

    Writes a line terminator asynchronously to the stream.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteLineAsync()

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteLineAsync(Char)

    Writes a character followed by a line terminator asynchronously to the stream.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteLineAsync(char value)

    Parameters

    • value

      The character to write to the stream.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteLineAsync(Char[], Int32, Int32)

    Writes a subarray of characters followed by a line terminator asynchronously to the stream.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteLineAsync(char[] buffer, int index, int count)

    Parameters

    • buffer

      The character array to write data from.

    • index

      The character position in the buffer at which to start reading data.

    • count

      The maximum number of characters to write.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteLineAsync(String)

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

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteLineAsync(string value)

    Parameters

    • value

      The string to write. If the value is null, only a line terminator is written.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

Details

Namespace

System.IO

Assembly

System.IO.dll

See Also