System.IO.StreamReader Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Implements a TextReader that reads characters from a byte stream in a particular encoding.
C# Syntax:
[Serializable]
public class StreamReader : TextReader
Remarks
StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.

StreamReader defaults to UTF-8 encoding unless specified otherwise, instead of defaulting to the ANSI code page for the current system. UTF-8 handles Unicode characters correctly and provides consistent results on localized versions of the operating system.

By default, a StreamReader is not thread safe. See TextReader.Synchronized for a thread-safe wrapper.

StreamReader.Read (char[], int, int) and StreamWriter.Write (char[], int, int) read and write the number of characters specified by the count parameter. These are to be distinguished from BufferedStream.Read and BufferedStream.Write, which read and write the number of bytes specified by the count parameter. Use the BufferedStream methods only for reading and writing an integral number of byte array elements.



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.
See also:
System.IO Namespace | Encoding | Stream | StreamWriter

System.IO.StreamReader Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(Stream stream)

Initializes a new instance of the StreamReader class for the specified stream.
ctor #2 Overloaded:
.ctor(string path)

Initializes a new instance of the StreamReader class for the specified file name.
ctor #3 Overloaded:
.ctor(Stream stream, bool detectEncodingFromByteOrderMarks)

Initializes a new instance of the StreamReader class the specified stream, with the specified byte order mark detection option.
ctor #4 Overloaded:
.ctor(Stream stream, Encoding encoding)

Initializes a new instance of the StreamReader class for the specified stream with the specified character encoding.
ctor #5 Overloaded:
.ctor(string path, bool detectEncodingFromByteOrderMarks)

Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.
ctor #6 Overloaded:
.ctor(string path, Encoding encoding)

Initializes a new instance of the StreamReader class for the specified file name and with the specified character encoding.
ctor #7 Overloaded:
.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)

Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.
ctor #8 Overloaded:
.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)

Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.
ctor #9 Overloaded:
.ctor(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)

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.
ctor #10 Overloaded:
.ctor(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)

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 Fields
Null A StreamReader around an empty stream.
Public Properties
BaseStream Read-only

Returns the underlying stream.
CurrentEncoding Read-only

Gets the current character encoding that the current StreamReader is using.
Public Methods
Close Overridden:
Closes the StreamReader and releases any system resources associated with the reader.
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
DiscardBufferedData Allows a StreamReader to discard its current data.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
Peek Overridden:
Returns the next available character but does not consume it.
Read Overloaded:
Read()

Overridden:
Reads the next character from the input stream and advances the character position by one character.
Read Overloaded:
Read(in char[] buffer, int index, int count)

Overridden:
Reads a maximum of count characters from the current stream into buffer, beginning at index.
ReadBlock
(inherited from System.IO.TextReader)
See base class member description: System.IO.TextReader.ReadBlock


Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
ReadLine Overridden:
Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd Overridden:
Reads the stream from the current position to the end of the stream.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Dispose Overridden:
Releases the unmanaged resources used by the StreamReader and optionally releases the managed resources.
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IO.StreamReader Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the StreamReader class for the specified stream.
C# Syntax:
public StreamReader(
   Stream stream
);
Parameters:

stream

The stream to be read.

Exceptions
Exception Type Condition
ArgumentException stream does not support reading.
ArgumentNullException stream is null.
Remarks
This constructor initializes the encoding to UTF8Encoding, the StreamReader.BaseStream property using the stream parameter, and the internal buffer to the default size. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the StreamReader class for the specified file name.
C# Syntax:
public StreamReader(
   string path
);
Parameters:

path

The complete file path to be read.

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path is null.
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The directory cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.
Remarks
The complete file path is specified by path. The default character encoding and default buffer size are used.

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

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

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the StreamReader class the specified stream, with the specified byte order mark detection option.
C# Syntax:
public StreamReader(
   Stream stream,
   bool detectEncodingFromByteOrderMarks
);
Parameters:

stream

The stream to be read.

detectEncodingFromByteOrderMarks

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

Exceptions
Exception Type Condition
ArgumentException stream does not support reading.
ArgumentNullException stream is null.
Remarks
This constructor initializes the encoding to UTF8Encoding, the StreamReader.BaseStream property using the stream parameter, and the internal buffer to the default size.

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

Example
		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"));
		}

    

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the StreamReader class for the specified stream with the specified character encoding.
C# Syntax:
public StreamReader(
   Stream stream,
   Encoding encoding
);
Parameters:

stream

The stream to be read.

encoding

The character encoding to use.

Exceptions
Exception Type Condition
ArgumentException stream does not support reading.
ArgumentNullException stream or encoding is null.
Remarks
The character encoding is set by encoding, and the default buffer size is used. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the StreamReader class for the specified file name, with the specified byte order mark detection option.
C# Syntax:
public StreamReader(
   string path,
   bool detectEncodingFromByteOrderMarks
);
Parameters:

path

The complete file path to be read.

detectEncodingFromByteOrderMarks

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

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path is null.
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The directory cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.
Remarks
This constructor initializes the encoding to UTF8Encoding, the StreamReader.BaseStream property using the stream parameter, and the internal buffer to the default size.

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

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

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

Example
		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"));
		}

    

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the StreamReader class for the specified file name and with the specified character encoding.
C# Syntax:
public StreamReader(
   string path,
   Encoding encoding
);
Parameters:

path

The complete file path to be read.

encoding

The character encoding to use.

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path or encoding is null.
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The directory cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.
Remarks
This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer to the default size.

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

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

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #7
Summary
Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding and byte order mark detection option.
C# Syntax:
public StreamReader(
   Stream stream,
   Encoding encoding,
   bool detectEncodingFromByteOrderMarks
);
Parameters:

stream

The stream to be read.

encoding

The character encoding to use.

detectEncodingFromByteOrderMarks

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

Exceptions
Exception Type Condition
ArgumentException stream does not support reading.
ArgumentNullException stream or encoding is null.
Remarks
This constructor initializes the encoding as specified by the encoding parameter, the StreamReader.BaseStream property using the stream parameter, and the internal buffer to the default size.

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

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #8
Summary
Initializes a new instance of the StreamReader class for the specified file name, with the specified character encoding and byte order mark detection option.
C# Syntax:
public StreamReader(
   string path,
   Encoding encoding,
   bool detectEncodingFromByteOrderMarks
);
Parameters:

path

The complete file path to be read.

encoding

The character encoding to use.

detectEncodingFromByteOrderMarks

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

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path or encoding is null.
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The directory cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.
Remarks
This constructor initializes the encoding as specified by the encoding parameter, and the internal buffer to the default size.

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

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

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

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #9
Summary
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.
C# Syntax:
public StreamReader(
   Stream stream,
   Encoding encoding,
   bool detectEncodingFromByteOrderMarks,
   int bufferSize
);
Parameters:

stream

The stream to be read.

encoding

The character encoding to use.

detectEncodingFromByteOrderMarks

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

bufferSize

The minimum buffer size.

Exceptions
Exception Type Condition
ArgumentException The stream does not support reading.
ArgumentNullException stream or encoding is null.
ArgumentOutOfRangeException bufferSize is less than or equal to zero.
Remarks
The buffer size, in number of 16-bit characters, is set by bufferSize. 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. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.



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. When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Overloaded ctor #10
Summary
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.
C# Syntax:
public StreamReader(
   string path,
   Encoding encoding,
   bool detectEncodingFromByteOrderMarks,
   int bufferSize
);
Parameters:

path

The complete file path to be read.

encoding

The character encoding to use.

detectEncodingFromByteOrderMarks

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

bufferSize

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

Exceptions
Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path or encoding is null.
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The directory cannot be found.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.
ArgumentOutOfRangeException buffersize is less than or equal to zero.
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. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.

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

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

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

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters may not be interpretable, and could cause an exception to be thrown.
Example
		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"));
		}

    
See also:
Encoding

Return to top


Field: Null
Summary
A StreamReader around an empty stream.
C# Syntax:
public static readonly StreamReader Null;
Remarks
When read methods are invoked on StreamReader.Null, zero is always returned. When StreamReader.ReadLine is invoked on StreamReader.Null, null is returned.
Example
			StreamReader srNull = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
				System.Text.Encoding.ASCII);
			if(!srNull.Equals(StreamReader.Null)) {
				srNull.BaseStream.Seek(0, SeekOrigin.Begin);
				Console.WriteLine(srNull.ReadToEnd());
			}
			srNull.Close();

    

Return to top


Property: BaseStream (read-only)
Summary
Returns the underlying stream.
C# Syntax:
public virtual Stream BaseStream {get;}
Remarks
StreamReader might buffer input such that the position of the underlying stream will not match the StreamReader position. The StreamReader constructors with the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader.
Example
This example shows a use of BaseStream with Stream.Seek and SeekOrigin to set the file pointer of the underlying stream to the beginning.
 FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
    FileAccess.Read);
 // Create a Char reader.
 StreamReader w = new StreamReader(fs);
 // Set the StreamReader file pointer to the end.
 w.BaseStream.Seek(0, SeekOrigin.End);

    

Return to top


Property: CurrentEncoding (read-only)
Summary
Gets the current character encoding that the current StreamReader is using.
C# Syntax:
public virtual Encoding CurrentEncoding {get;}
Example
			StreamReader srEncoding = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
				System.Text.Encoding.ASCII);
			Console.WriteLine("Encoding: {0}", 
				srEncoding.CurrentEncoding.EncodingName);
			srEncoding.Close();

    

Return to top


Overridden Method: Close()
Summary
Closes the StreamReader and releases any system resources associated with the reader.
C# Syntax:
public override void Close();
Remarks
This method overrides TextWriter.Close.

This implementation of Close calls the StreamReader.Dispose method passing a true value.

Flushing the stream will not flush its underlying encoder unless you explicitly call Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Following a call to Close, any operations on the reader might raise exceptions.

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Method: DiscardBufferedData()
Summary
Allows a StreamReader to discard its current data.
C# Syntax:
public void DiscardBufferedData();
Remarks
Because Read does not update the current position of the underlying stream, instances of classes such as StringReader return more characters than are actually in the stream. Use DiscardBufferedData to discard this excess data.

Return to top


Overridden Method: Dispose(
   bool disposing
)
Summary
Releases the unmanaged resources used by the StreamReader and optionally releases the managed resources.
C# Syntax:
protected override void Dispose(
   bool disposing
);
Parameters:

disposing

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks
When the disposing parameter is true, this method releases all resources held by any managed objects that this System.IO.StreamReader references. This method invokes the Dispose method of each referenced object.

Notes to inheritors: Dispose may be called multiple times by other objects. When overriding Dispose, be careful not to reference objects that have been previously disposed in an earlier call to Dispose.

This method calls the dispose method of the base class, Dispose.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~StreamReader();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: Peek()
Summary
Returns the next available character but does not consume it.
C# Syntax:
public override int Peek();
Return Value:
The next character to be read, or -1 if no more characters are available or the stream does not support seeking.
Exceptions
Exception Type Condition
IOException An I/O error occurs.
Remarks
This method overrides TextReader.Peek.

The current position of the StreamReader is not changed by Peek. The returned value is -1 if no more characters are currently available.

Example
			StreamReader srPeek = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
				System.Text.Encoding.ASCII);
			// set the file pointer to the beginning
			srPeek.BaseStream.Seek(0, SeekOrigin.Begin);
			// cycle while there is a next char
			while (srPeek.Peek() > -1) {
				Console.Write(srPeek.ReadLine());
			}
			// close the reader and the file
			srPeek.Close();

    

Return to top


Overloaded Method: Read()
Summary
Reads the next character from the input stream and advances the character position by one character.
C# Syntax:
public override int Read();
Return Value:
The next character from the input stream represented as an Int32, or -1 if no more characters are available.
Exceptions
Exception Type Condition
IOException An I/O error occurs.
Remarks
This method overrides TextReader.Read.

Since attempting to read beyond the end of the stream is not an exceptional case, the return type of this method is an integer, allowing a return value of -1 that signifies reading beyond the end of the stream.

Example

Return to top


Overloaded Method: Read(
   in char[] buffer,
   int index,
   int count
)
Summary
Reads a maximum of count characters from the current stream into buffer, beginning at index.
C# Syntax:
public override int Read(
   in char[] buffer,
   int index,
   int count
);
Parameters:

buffer

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

index

The index of buffer at which to begin writing.

count

The maximum number of characters to read.

Return Value:
The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to count, depending on whether the data is available within the stream.
Exceptions
Exception Type Condition
ArgumentException The buffer length minus index is less than count.
ArgumentNullException buffer is null.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs, such as the stream is closed.
Remarks
This method overrides TextReader.Read.

Since attempting to read beyond the end of the stream is not an exceptional case, the return type of this method is an integer, allowing a return value of -1 that signifies reading beyond the end of the stream.

When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

This method returns after either count characters are read, or the end of the file is reached. TextReader.ReadBlock is a blocking version of StreamReader.Read.

Example
			StreamReader srRead = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
				System.Text.Encoding.ASCII);
			// set the file pointer to the beginning
			srRead.BaseStream.Seek(0, SeekOrigin.Begin);
			srRead.BaseStream.Position = 0;
			while (srRead.BaseStream.Position < srRead.BaseStream.Length) {
				char[] buffer = new char[1];
				srRead.Read(buffer, 0, 1);
				Console.Write(buffer[0].ToString());
				srRead.BaseStream.Position++;
			}
			srRead.DiscardBufferedData();
			srRead.Close();

    

Return to top


Method: ReadBlock(
   in char[] buffer,
   int index,
   int count
)
Inherited
See base class member description: System.IO.TextReader.ReadBlock

Summary
Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
C# Syntax:
public virtual int ReadBlock(
   in char[] buffer,
   int index,
   int count
);
Parameters:

buffer

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

index

The place in buffer at which to begin writing.

count

The maximum number of characters to read.

Return Value:
The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.
Exceptions
Exception Type Condition
ArgumentNullException buffer is null.
ArgumentException The buffer length minus index is less than count.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs.
Remarks
The method blocks until either count characters are read, or all characters have been read. This is a blocking version of TextReader.Read.

Return to top


Overridden Method: ReadLine()
Summary
Reads a line of characters from the current stream and returns the data as a string.
C# Syntax:
public override string ReadLine();
Return Value:
The next line from the input stream, or null if the end of the input stream is reached.
Exceptions
Exception Type Condition
OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
IOException An I/O error occurs.
Remarks
A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached.

This method overrides TextReader.ReadLine.

If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal StreamReader.ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.

To avoid such a situation and produce robust code you should use the StreamReader.Read method and store the read characters in a preallocated buffer.

Example
			StreamReader srReadLine = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
			System.Text.Encoding.ASCII);
			srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);
			while (srReadLine.Peek() > -1) {
				Console.WriteLine(srReadLine.ReadLine());
			}
			srReadLine.Close();

    

Return to top


Overridden Method: ReadToEnd()
Summary
Reads the stream from the current position to the end of the stream.
C# Syntax:
public override string ReadToEnd();
Return Value:
The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns the empty string("").
Exceptions
Exception Type Condition
OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
IOException An I/O error occurs.
Remarks
This method overrides TextReader.ReadToEnd.

ReadToEnd works best when you need to read all the input from the current position to the end of the stream. If more control is needed over how many characters are read from the stream, use StreamReader.Read (char[], int, int), which generally results in better performance.

Note than when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

If the current method throws an OutOfMemoryException, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters already read into the internal StreamReader.ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.

To avoid such a situation and produce robust code you should use the StreamReader.Read method and store the read characters in a preallocated buffer.

Example
			StreamReader srReadToEnd = new StreamReader(
				(System.IO.Stream)File.OpenRead("C:\\Temp\\Test.txt"),
			System.Text.Encoding.ASCII);
			srReadToEnd.BaseStream.Seek(0, SeekOrigin.Begin);
			Console.WriteLine(srReadToEnd.ReadToEnd());
			srReadToEnd.Close();

    

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.