This documentation is archived and is not being maintained.

TextReader Class

Represents a reader that can read a sequential series of characters.

For a list of all members of this type, see TextReader Members.


[Visual Basic]
MustInherit Public Class TextReader
   Inherits MarshalByRefObject
   Implements IDisposable
public abstract class TextReader : MarshalByRefObject, IDisposable
public __gc __abstract class TextReader : public
   MarshalByRefObject, IDisposable
abstract class TextReader extends MarshalByRefObject
   implements IDisposable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


TextReader is the abstract base class of StreamReader and StringReader, which read characters from streams and strings, respectively. Create an instance of TextReader to open a text file for reading a specified range of characters, or to create a reader based on an existing stream. You can also use an instance of TextReader to read text from a custom backing store using the same APIs you would use for a string or a stream.

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

A derived class must minimally implement the Peek and Read methods in order to make a useful instance of TextReader.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File


[Visual Basic, C#, C++] The following code example demonstrates the polymorphic behavior of the TextReader and TextWriter types. Since aStringWriter and aStreamWriter are both TextWriter types, the WriteVowel method is called with both objects and the Write methods associated with each specific type are executed. Similarly, the ReadText method is called with both aStringReader and aStreamReader, and the correct ReadToEnd method is executed. Note that for aStringWriter and aStringReader, the backing store is a string, while a file is the backing store for aStreamWriter and aStreamReader.

[Visual Basic] 
Imports System
Imports System.IO

Public Class TextRW

    Shared Sub Main()
        Dim aStringWriter, aStreamWriter As TextWriter
        aStringWriter = New StringWriter()
        aStreamWriter = New StreamWriter("InvalidPathChars.txt")
        Dim aStringReader, aStreamReader As TextReader
        aStringReader = New StringReader(aStringWriter.ToString())
        aStreamReader = New StreamReader("InvalidPathChars.txt")
    End Sub

    Shared Sub WriteText(aTextWriter As TextWriter)
        aTextWriter.Write("Invalid file path characters are: ")
    End Sub

    Shared Sub ReadText(aTextReader As TextReader)
        Console.WriteLine("From {0} - {1}", _
            aTextReader.GetType().Name, aTextReader.ReadToEnd())
    End Sub

End Class

using System;
using System.IO;

class TextRW
    static void Main()
        TextWriter stringWriter = new StringWriter();
        using(TextWriter streamWriter = 
            new StreamWriter("InvalidPathChars.txt"))

        TextReader stringReader = 
            new StringReader(stringWriter.ToString());
        using(TextReader streamReader = 
            new StreamReader("InvalidPathChars.txt"))

    static void WriteText(TextWriter textWriter)
        textWriter.Write("Invalid file path characters are: ");

    static void ReadText(TextReader textReader)
        Console.WriteLine("From {0} - {1}", 
            textReader.GetType().Name, textReader.ReadToEnd());

#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

static void WriteText(TextWriter* textWriter)
    textWriter->Write("Invalid file path characters are: ");

static void ReadText(TextReader* textReader)
    Console::WriteLine(S"From {0} - {1}", 
        textReader->GetType()->Name, textReader->ReadToEnd());

void main()
    TextWriter* stringWriter = new StringWriter(); 
    TextWriter* streamWriter = 
        new StreamWriter("InvalidPathChars.txt");


    TextReader* stringReader = 
        new StringReader(stringWriter->ToString()); 
    TextReader* streamReader = 
        new StreamReader("InvalidPathChars.txt");


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


Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

TextReader Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File