Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

StreamReader Constructor

.NET Framework 1.1

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

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream)
[C#] public StreamReader(Stream);
[C++] public: StreamReader(Stream*);
[JScript] public function StreamReader(Stream);

Initializes a new instance of the StreamReader class for the specified file name.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String)
[C#] public StreamReader(string);
[C++] public: StreamReader(String*);
[JScript] public function StreamReader(String);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Boolean)
[C#] public StreamReader(Stream, bool);
[C++] public: StreamReader(Stream*, bool);
[JScript] public function StreamReader(Stream, Boolean);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding)
[C#] public StreamReader(Stream, Encoding);
[C++] public: StreamReader(Stream*, Encoding*);
[JScript] public function StreamReader(Stream, Encoding);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Boolean)
[C#] public StreamReader(string, bool);
[C++] public: StreamReader(String*, bool);
[JScript] public function StreamReader(String, Boolean);

Initializes a new instance of the StreamReader class for the specified file name and with the specified character encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Encoding)
[C#] public StreamReader(string, Encoding);
[C++] public: StreamReader(String*, Encoding*);
[JScript] public function StreamReader(String, Encoding);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding, Boolean)
[C#] public StreamReader(Stream, Encoding, bool);
[C++] public: StreamReader(Stream*, Encoding*, bool);
[JScript] public function StreamReader(Stream, Encoding, Boolean);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Encoding, Boolean)
[C#] public StreamReader(string, Encoding, bool);
[C++] public: StreamReader(String*, Encoding*, bool);
[JScript] public function StreamReader(String, Encoding, Boolean);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding, Boolean, Integer)
[C#] public StreamReader(Stream, Encoding, bool, int);
[C++] public: StreamReader(Stream*, Encoding*, bool, int);
[JScript] public function StreamReader(Stream, Encoding, Boolean, int);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(String, Encoding, Boolean, Integer)
[C#] public StreamReader(string, Encoding, bool, int);
[C++] public: StreamReader(String*, Encoding*, bool, int);
[JScript] public function StreamReader(String, Encoding, Boolean, int);

Example

[Visual Basic, C#, C++] The following example demonstrates this StreamReader constructor.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the StreamReader constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub getNewStreamReader()
    Dim S As Stream = File.OpenRead("C:\Temp\Test.txt")
    'Get a new StreamReader in ASCII format from a
    'file using a buffer and byte order mark detection
    Dim SrAsciiFromFileFalse512 As StreamReader = 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
    Dim SrAsciiFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a file 
    Dim SrAsciiFromFile As StreamReader = New StreamReader("C:\Temp\Test.txt", _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'file with byte order mark detection = false        
    Dim SrFromFileFalse As StreamReader = New StreamReader("C:\Temp\Test.txt", False)
    'Get a new StreamReader from a file
    Dim SrFromFile As StreamReader = 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
    Dim SrAsciiFromStreamFalse512 As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII, False, 512)
    'Get a new StreamReader in ASCII format from a
    'FileStream with byte order mark detection = false
    Dim SrAsciiFromStreamFalse = New StreamReader(S, _
        System.Text.Encoding.ASCII, False)
    'Get a new StreamReader in ASCII format from a FileStream
    Dim SrAsciiFromStream As StreamReader = New StreamReader(S, _
        System.Text.Encoding.ASCII)
    'Get a new StreamReader from a
    'FileStream with byte order mark detection = false
    Dim SrFromStreamFalse As StreamReader = New StreamReader(S, False)
    'Get a new StreamReader from a FileStream
    Dim SrFromStream As StreamReader = New StreamReader(S)

End Sub

[C#] 
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"));
}

[C++] 
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(
            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(
            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(
            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(
            File::OpenRead("C:\\Temp\\Test.txt"), 
            false);
        //Get a new StreamReader from a FileStream
        StreamReader *srFromStream = new StreamReader(
            File::OpenRead("C:\\Temp\\Test.txt"));
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

StreamReader Class | StreamReader Members | System.IO Namespace

Show:
© 2015 Microsoft