Uredi

Deli z drugimi prek


StreamReader Constructors

Definition

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

Overloads

StreamReader(Stream)

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

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.

StreamReader(String, Encoding, Boolean, FileStreamOptions)

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

StreamReader(String, Encoding, Boolean, Int32)

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.

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.

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.

StreamReader(String, 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.

StreamReader(String, FileStreamOptions)

Initializes a new instance of the StreamReader class for the specified file path, using the default encoding, enabling detection of byte order marks at the beginning of the file, and configured with the specified FileStreamOptions object.

StreamReader(String, Boolean)

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

StreamReader(Stream, Encoding)

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

StreamReader(Stream, Boolean)

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

StreamReader(String)

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

StreamReader(String, Encoding)

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

StreamReader(Stream)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::IO::Stream ^ stream);
public StreamReader (System.IO.Stream stream);
new System.IO.StreamReader : System.IO.Stream -> System.IO.StreamReader
Public Sub New (stream As Stream)

Parameters

stream
Stream

The stream to be read.

Exceptions

stream does not support reading.

stream is null.

Examples

The following code example demonstrates this StreamReader constructor.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      FileStream^ fs = gcnew FileStream( path,FileMode::Open );
      try
      {
         StreamReader^ sr = gcnew StreamReader( fs );
         try
         {
            while ( sr->Peek() >= 0 )
            {
               Console::WriteLine( sr->ReadLine() );
            }
         }
         finally
         {
            delete sr;
         }
      }
      finally
      {
         delete fs;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.CreateNew)))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("reading");
            }

            using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open)))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim fs As FileStream = New FileStream(path, FileMode.Open)

            Dim sr As StreamReader = New StreamReader(fs)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
            fs.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

Remarks

This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

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

Caution

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.

See also

Applies to

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

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen);
public StreamReader (System.IO.Stream stream, System.Text.Encoding? encoding = default, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional detectEncodingFromByteOrderMarks As Boolean = true, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)

Parameters

stream
Stream

The stream to read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

bufferSize
Int32

The minimum buffer size.

leaveOpen
Boolean

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

Remarks

Unless you set the leaveOpen parameter to true, the StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.

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 enables you to change the encoding the first time you read from the StreamReader object. If the detectEncodingFromByteOrderMarks parameter is true, the constructor detects the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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.

Note

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

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 interpreted correctly, and could cause an exception to be thrown.

Applies to

StreamReader(String, Encoding, Boolean, FileStreamOptions)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.Text.Encoding * bool * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, options As FileStreamOptions)

Parameters

path
String

The complete file path to be read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

options
FileStreamOptions

An object that specifies the configuration options for the underlying FileStream.

Exceptions

stream is not readable.

      -or-

path is an empty string ("").

path or encoding or options is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

See also

Applies to

StreamReader(String, Encoding, Boolean, Int32)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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.

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

Parameters

path
String

The complete file path to be read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

bufferSize
Int32

The minimum buffer size, in number of 16-bit characters.

Exceptions

path is an empty string ("").

path or encoding is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

buffersize is less than or equal to zero.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding as specified by the encoding parameter.

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 four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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 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.

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

Caution

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.

See also

Applies to

StreamReader(Stream, Encoding, Boolean, Int32)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean, bufferSize As Integer)

Parameters

stream
Stream

The stream to be read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

bufferSize
Int32

The minimum buffer size.

Exceptions

The stream does not support reading.

stream or encoding is null.

bufferSize is less than or equal to zero.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

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 four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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.

Note

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

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.

See also

Applies to

StreamReader(Stream, Encoding, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

Parameters

stream
Stream

The stream to be read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

Exceptions

stream does not support reading.

stream or encoding is null.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding as specified by the encoding parameter, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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.

Caution

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.

See also

Applies to

StreamReader(String, Encoding, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding, bool detectEncodingFromByteOrderMarks);
public StreamReader (string path, System.Text.Encoding encoding, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * System.Text.Encoding * bool -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding, detectEncodingFromByteOrderMarks As Boolean)

Parameters

path
String

The complete file path to be read.

encoding
Encoding

The character encoding to use.

detectEncodingFromByteOrderMarks
Boolean

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

Exceptions

path is an empty string ("").

path or encoding is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer size to 1024 bytes.

The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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 path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

Caution

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.

See also

Applies to

StreamReader(String, FileStreamOptions)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

Initializes a new instance of the StreamReader class for the specified file path, using the default encoding, enabling detection of byte order marks at the beginning of the file, and configured with the specified FileStreamOptions object.

public:
 StreamReader(System::String ^ path, System::IO::FileStreamOptions ^ options);
public StreamReader (string path, System.IO.FileStreamOptions options);
new System.IO.StreamReader : string * System.IO.FileStreamOptions -> System.IO.StreamReader
Public Sub New (path As String, options As FileStreamOptions)

Parameters

path
String

The complete file path to be read.

options
FileStreamOptions

An object that specifies the configuration options for the underlying FileStream.

Exceptions

stream is not readable.

-or-

      <code data-dev-comment-type="paramref">path</code> is an empty string ("").

path or options is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

See also

Applies to

StreamReader(String, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path, bool detectEncodingFromByteOrderMarks);
public StreamReader (string path, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : string * bool -> System.IO.StreamReader
Public Sub New (path As String, detectEncodingFromByteOrderMarks As Boolean)

Parameters

path
String

The complete file path to be read.

detectEncodingFromByteOrderMarks
Boolean

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

Exceptions

path is an empty string ("").

path is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. Otherwise, the UTF8Encoding is used. See the Encoding.GetPreamble method for more information.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

StreamReader(Stream, Encoding)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding);
public StreamReader (System.IO.Stream stream, System.Text.Encoding encoding);
new System.IO.StreamReader : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (stream As Stream, encoding As Encoding)

Parameters

stream
Stream

The stream to be read.

encoding
Encoding

The character encoding to use.

Exceptions

stream does not support reading.

stream or encoding is null.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

The character encoding is set by the encoding parameter, and the buffer size is set to 1024 bytes. The StreamReader object attempts to detect the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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.

Caution

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.

See also

Applies to

StreamReader(Stream, Boolean)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::IO::Stream ^ stream, bool detectEncodingFromByteOrderMarks);
public StreamReader (System.IO.Stream stream, bool detectEncodingFromByteOrderMarks);
new System.IO.StreamReader : System.IO.Stream * bool -> System.IO.StreamReader
Public Sub New (stream As Stream, detectEncodingFromByteOrderMarks As Boolean)

Parameters

stream
Stream

The stream to be read.

detectEncodingFromByteOrderMarks
Boolean

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

Exceptions

stream does not support reading.

stream is null.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding to UTF8Encoding, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes.

The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 text if the file starts with the appropriate byte order marks. See the Encoding.GetPreamble method for more information.

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

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

StreamReader(String)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path);
public StreamReader (string path);
new System.IO.StreamReader : string -> System.IO.StreamReader
Public Sub New (path As String)

Parameters

path
String

The complete file path to be read.

Exceptions

path is an empty string ("").

path is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

Examples

The following code example demonstrates this StreamReader constructor.

using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      if ( File::Exists( path ) )
      {
         File::Delete( path );
      }
      StreamWriter^ sw = gcnew StreamWriter( path );
      try
      {
         sw->WriteLine( "This" );
         sw->WriteLine( "is some text" );
         sw->WriteLine( "to test" );
         sw->WriteLine( "Reading" );
      }
      finally
      {
         delete sw;
      }

      StreamReader^ sr = gcnew StreamReader( path );
      try
      {
         while ( sr->Peek() >= 0 )
         {
            Console::WriteLine( sr->ReadLine() );
         }
      }
      finally
      {
         delete sr;
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
using System;
using System.IO;

class Test
{
    
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        Try
            If File.Exists(path) Then
                File.Delete(path)
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("This")
            sw.WriteLine("is some text")
            sw.WriteLine("to test")
            sw.WriteLine("Reading")
            sw.Close()

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

Remarks

The complete file path is specified by the path parameter. This constructor initializes the encoding to UTF8Encoding and the buffer size to 1024 bytes.

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

Caution

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.

See also

Applies to

StreamReader(String, Encoding)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

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

public:
 StreamReader(System::String ^ path, System::Text::Encoding ^ encoding);
public StreamReader (string path, System.Text.Encoding encoding);
new System.IO.StreamReader : string * System.Text.Encoding -> System.IO.StreamReader
Public Sub New (path As String, encoding As Encoding)

Parameters

path
String

The complete file path to be read.

encoding
Encoding

The character encoding to use.

Exceptions

path is an empty string ("").

path or encoding is null.

The file cannot be found.

The specified path is invalid, such as being on an unmapped drive.

path includes an incorrect or invalid syntax for file name, directory name, or volume label.

Examples

The following code example demonstrates this StreamReader constructor.

void getNewStreamReader()
{
   
   //Get a new StreamReader in ASCII format from a
   //file using a buffer and byte order mark detection
   StreamReader^ srAsciiFromFileFalse512 = gcnew 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 = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a file 
   StreamReader^ srAsciiFromFile = gcnew StreamReader(  "C:\\Temp\\Test.txt",System::Text::Encoding::ASCII );
   
   //Get a new StreamReader from a
   //file with byte order mark detection = false
   StreamReader^ srFromFileFalse = gcnew StreamReader(  "C:\\Temp\\Test.txt",false );
   
   //Get a new StreamReader from a file
   StreamReader^ srFromFile = gcnew 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 = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),System::Text::Encoding::ASCII,false );
   
   //Get a new StreamReader in ASCII format from a FileStream
   StreamReader^ srAsciiFromStream = gcnew 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 = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ),false );
   
   //Get a new StreamReader from a FileStream
   StreamReader^ srFromStream = gcnew StreamReader( File::OpenRead(  "C:\\Temp\\Test.txt" ) );
}
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"));
}
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

Remarks

This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer size to 1024 bytes. The StreamReader object attempts to detect the encoding by looking at the first four bytes of the stream. It automatically recognizes UTF-8, little-endian UTF-16, big-endian UTF-16, little-endian UTF-32, and big-endian UTF-32 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 path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

The path parameter is not required to be a file stored on disk; it can be any part of a system that supports access using streams.

Caution

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.

See also

Applies to