MSDN

.NET API Reference

System.StreamReader Class

Namespace: System.IO

Assembly: System.Runtime.dll

The StreamReader class implements a TextReader that reads characters from a byte stream in a particular encoding.

Syntax

public sealed class StreamReader : TextReader
{
    // Constructors
    public StreamReader(Stream stream);
    public StreamReader(Stream stream, Encoding encoding);
    public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks);
    public StreamReader(string path);
    public StreamReader(string path, Encoding encoding);
    public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks);
    public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
    public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool useAsync);
    
    // Properties
    public Stream BaseStream { get; }
    public Encoding CurrentEncoding { get; }
    public bool EndOfStream { get; }

    // Methods
    public override void Close();
    public void DiscardBufferedData();
    public override int Peek();
    public override int Read();
    public override int Read(char[] buffer, int index, int count);
    public string ReadLine();
    public string ReadToEnd();
    public static StreamReader Null { get; }
}

Members

MemberDescription
StreamReader(Stream)Initializes a new instance of the StreamReader class for the specified stream.
StreamReader(string)Initializes a new instance of the StreamReader class for the specified file.
BaseStreamGets the underlying stream.
CurrentEncodingGets the current character encoding.
EndOfStreamIndicates whether the current position is at the end of the stream.
ReadLine()Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd()Reads all characters from the current position to the end of the stream.
Peek()Returns the next available character but does not consume it.
Read()Reads the next character from the input stream and advances the character position by one character.
Read(char[] buffer, int index, int count)Reads a maximum of count characters from the current stream into a buffer, beginning at the specified index.

Remarks

The StreamReader class provides efficient reading of characters and lines from a stream, handling encoding detection and byte order marks when necessary. It is commonly used for reading text files, network streams, and other sources where textual data is stored as bytes.

When dealing with large files, consider using the asynchronous methods (ReadLineAsync, ReadToEndAsync) available through TextReader to avoid blocking the calling thread.

Example

The following example reads a text file line by line and prints each line to the console.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        const string path = "example.txt";

        using (var reader = new StreamReader(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}

See Also