StreamReader Class

Implements a TextReader that reads characters from a byte stream in a particular encoding.

Syntax

Declaration

[Serializable]
[ComVisible(true)]
public class StreamReader : TextReader, IDisposable

Inheritance Hierarchy

Inherited Members

System.IO.TextReader

System.Object

, , , , , ,

Constructors summary

Initializes a new instance of the StreamReader class for the specified stream.

Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.

Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.

Fields summary

A StreamReader object around an empty stream.

Properties summary

Returns the underlying stream.

Gets the current character encoding that the current StreamReader object is using.

Gets a value that indicates whether the current stream position is at the end of the stream.

Methods summary

Clears the internal buffer.

Closes the underlying stream, releases the unmanaged resources used by the StreamReader, and optionally releases the managed resources.

Returns the next available character but does not consume it.

Reads the next character from the input stream and advances the character position by one character.

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.

Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

Reads a line of characters from the current stream and returns the data as a string.

Reads a line of characters asynchronously from the current stream and returns the data as a string.

Reads all characters from the current position to the end of the stream.

Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.

Constructors

  • StreamReader(Stream)

    Initializes a new instance of the StreamReader class for the specified stream.

    public StreamReader(Stream stream)

    Parameters

    • stream

      The stream to be read.

    Exceptions

  • StreamReader(Stream, Boolean)

    Initializes a new instance of the StreamReader class for the specified stream, with the specified byte order mark detection option.

    public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)

    Parameters

    • stream

      The stream to be read.

    • detectEncodingFromByteOrderMarks

      Indicates whether to look for byte order marks at the beginning of the file.

    Exceptions

  • StreamReader(Stream, Encoding)

    Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding.

    public StreamReader(Stream stream, Encoding encoding)

    Parameters

    • stream

      The stream to be read.

    • encoding

      The character encoding to use.

    Exceptions

  • StreamReader(Stream, Encoding, Boolean)

    Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.

    public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)

    Parameters

    • stream

      The stream to be read.

    • encoding

      The character encoding to use.

    • detectEncodingFromByteOrderMarks

      Indicates whether to look for byte order marks at the beginning of the file.

    Exceptions

  • StreamReader(Stream, Encoding, Boolean, Int32)

    Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding, byte order mark detection option, and buffer size.

    public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)

    Parameters

    • stream

      The stream to be read.

    • encoding

      The character encoding to use.

    • detectEncodingFromByteOrderMarks

      Indicates whether to look for byte order marks at the beginning of the file.

    • bufferSize

      The minimum buffer size.

    Exceptions

  • StreamReader(Stream, Encoding, Boolean, Int32, Boolean)

    Initializes a new instance of the StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.

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

    Parameters

    • stream

      The stream to read.

    • encoding

      The character encoding to use.

    • detectEncodingFromByteOrderMarks

      true to look for byte order marks at the beginning of the file; otherwise, false.

    • bufferSize

      The minimum buffer size.

    • leaveOpen

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

Fields

  • Null

    A StreamReader object around an empty stream.

    public static readonly StreamReader Null

Properties

  • BaseStream

    Returns the underlying stream.

    public virtual Stream BaseStream { get; }

    Property Value

    • The underlying stream.

  • CurrentEncoding

    Gets the current character encoding that the current StreamReader object is using.

    public virtual Encoding CurrentEncoding { get; }

    Property Value

    • The current character encoding used by the current reader. The value can be different after the first call to any Read() method of StreamReader, since encoding autodetection is not done until the first call to a Read() method.

  • EndOfStream

    Gets a value that indicates whether the current stream position is at the end of the stream.

    public bool EndOfStream { get; }

    Property Value

    • true if the current stream position is at the end of the stream; otherwise false.

    Exceptions

Methods

  • DiscardBufferedData()

    Clears the internal buffer.

    public void DiscardBufferedData()
  • Dispose(Boolean)

    Closes the underlying stream, releases the unmanaged resources used by the StreamReader, 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.

    Overrides

  • Peek()

    Returns the next available character but does not consume it.

    public override int Peek()

    Returns

    • An integer representing the next character to be read, or -1 if there are no characters to be read or if the stream does not support seeking.

    Exceptions

    Overrides

  • Read()

    Reads the next character from the input stream and advances the character position by one character.

    public override int Read()

    Returns

    • The next character from the input stream represented as an Int32 object, or -1 if no more characters are available.

    Exceptions

    Overrides

  • Read(Char[], Int32, Int32)

    Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

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

    Parameters

    • buffer

      When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

    • index

      The index of buffer at which to begin writing.

    • count

      The maximum number of characters to read.

    Returns

    • The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count parameter, depending on whether the data is available within the stream.

    Exceptions

    Overrides

  • ReadAsync(Char[], Int32, Int32)

    Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

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

    Parameters

    • buffer

      When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

    • index

      The position in buffer at which to begin writing.

    • count

      The maximum number of characters to read. If the end of the stream is reached before the specified number of characters is written into the buffer, the current method returns.

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

    Exceptions

    Overrides

  • ReadBlock(Char[], Int32, Int32)

    Reads a specified maximum number of characters from the current stream and writes the data to a buffer, beginning at the specified index.

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

    Parameters

    • buffer

      When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

    • index

      The position in buffer at which to begin writing.

    • count

      The maximum number of characters to read.

    Returns

    • The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.

    Exceptions

    Overrides

  • ReadBlockAsync(Char[], Int32, Int32)

    Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

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

    Parameters

    • buffer

      When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

    • index

      The position in buffer at which to begin writing.

    • count

      The maximum number of characters to read. If the end of the stream is reached before the specified number of characters is written into the buffer, the method returns.

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

    Exceptions

    Overrides

  • ReadLine()

    Reads a line of characters from the current stream and returns the data as a string.

    public override string ReadLine()

    Returns

    • The next line from the input stream, or null if the end of the input stream is reached.

    Exceptions

    Overrides

  • ReadLineAsync()

    Reads a line of characters asynchronously from the current stream and returns the data as a string.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task<string> ReadLineAsync()

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the stream, or is null if all the characters have been read.

    Exceptions

    Overrides

  • ReadToEnd()

    Reads all characters from the current position to the end of the stream.

    public override string ReadToEnd()

    Returns

    • The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns an empty string ("").

    Exceptions

    Overrides

  • ReadToEndAsync()

    Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task<string> ReadToEndAsync()

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains a string with the characters from the current position to the end of the stream.

    Exceptions

    Overrides

Details

Namespace

System.IO

Assembly

System.IO.dll

See Also