Export (0) Print
Expand All

StreamReader Constructor (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.

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

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

Parameters

stream
Type: System.IO.Stream

The stream to be read.

encoding
Type: System.Text.Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Type: System.Boolean

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

bufferSize
Type: System.Int32

The minimum buffer size.

ExceptionCondition
ArgumentException

The stream does not support reading.

ArgumentNullException

stream or encoding is null.

ArgumentOutOfRangeException

bufferSize is less than or equal to zero.

The buffer size, in number of 16-bit characters, is set by the bufferSize parameter. If bufferSize is less than the minimum allowable size (128 characters), the minimum allowable size is used.

This constructor allows you to change the encoding the first time you read from the StreamReader object. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.

The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

NoteNote

When reading from a Stream, it is more efficient to use a buffer that is the same size as the internal buffer of the stream.

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 this StreamReader constructor.

private void getNewStreamReader() 
{
    //Get a new StreamReader in ASCII format from a 
    //file using a buffer and byte order mark detection
    StreamReader srAsciiFromFileFalse512 = 
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a 
    //file with byte order mark detection = false
    StreamReader srAsciiFromFileFalse = 
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a file 
    StreamReader srAsciiFromFile = 
        new StreamReader("C:\\Temp\\Test.txt",
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a 
    //file with byte order mark detection = false
    StreamReader srFromFileFalse = 
        new StreamReader("C:\\Temp\\Test.txt", false);
    //Get a new StreamReader from a file
    StreamReader srFromFile = 
        new StreamReader("C:\\Temp\\Test.txt");
    //Get a new StreamReader in ASCII format from a 
    //FileStream with byte order mark detection = false and a buffer
    StreamReader srAsciiFromStreamFalse512 = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false, 512);
    //Get a new StreamReader in ASCII format from a 
    //FileStream with byte order mark detection = false
    StreamReader srAsciiFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII, false);
    //Get a new StreamReader in ASCII format from a FileStream
    StreamReader srAsciiFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
        System.Text.Encoding.ASCII);
    //Get a new StreamReader from a 
    //FileStream with byte order mark detection = false
    StreamReader srFromStreamFalse = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"), 
        false);
    //Get a new StreamReader from a FileStream
    StreamReader srFromStream = new StreamReader(
        (System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"));
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 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

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft