Skip to main content
.NET Framework Class Library
StringReader Class

Implements a TextReader that reads from a string.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_0_0_0%]> _
<[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_0_0_1%](True)> _
Public Class StringReader _
	Inherits [%$TOPIC/fzy4xe55_en-us_VS_110_3_0_0_0_2%]
[[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_1_0_0%]]
[[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_1_0_1%](true)]
public class StringReader : [%$TOPIC/fzy4xe55_en-us_VS_110_3_0_1_0_2%]
[[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_2_0_0%]]
[[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_2_0_1%](true)]
public ref class StringReader : public [%$TOPIC/fzy4xe55_en-us_VS_110_3_0_2_0_2%]
[<[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_3_0_0%]>]
[<[%$TOPIC/fzy4xe55_en-us_VS_110_3_0_3_0_1%](true)>]
type StringReader =  
    class 
        inherit [%$TOPIC/fzy4xe55_en-us_VS_110_3_0_3_0_2%] 
    end

The StringReader type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps StringReaderInitializes a new instance of the StringReader class that reads from the specified string.
Top
Methods
  NameDescription
Public method Supported by the XNA Framework CloseCloses the StringReader. (Overrides TextReaderClose.)
Public method CreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps DisposeReleases all resources used by the TextReader object. (Inherited from TextReader.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps Dispose(Boolean)Releases the unmanaged resources used by the StringReader and optionally releases the managed resources. (Overrides TextReaderDispose(Boolean).)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps PeekReturns the next available character but does not consume it. (Overrides TextReaderPeek.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ReadReads the next character from the input string and advances the character position by one character. (Overrides TextReaderRead.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps Read(Char, Int32, Int32)Reads a block of characters from the input string and advances the character position by count. (Overrides TextReaderRead(Char, Int32, Int32).)
Public method Supported in .NET for Windows Store apps ReadAsyncReads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index. (Overrides TextReaderReadAsync(Char, Int32, Int32).)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ReadBlockReads a specified maximum number of characters from the current text reader and writes the data to a buffer, beginning at the specified index. (Inherited from TextReader.)
Public method Supported in .NET for Windows Store apps ReadBlockAsyncReads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index. (Overrides TextReaderReadBlockAsync(Char, Int32, Int32).)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ReadLineReads a line of characters from the current string and returns the data as a string. (Overrides TextReaderReadLine.)
Public method Supported in .NET for Windows Store apps ReadLineAsyncReads a line of characters asynchronously from the current string and returns the data as a string. (Overrides TextReaderReadLineAsync.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ReadToEndReads all characters from the current position to the end of the string and returns them as a single string. (Overrides TextReaderReadToEnd.)
Public method Supported in .NET for Windows Store apps ReadToEndAsyncReads all characters from the current position to the end of the string asynchronously and returns them as a single string. (Overrides TextReaderReadToEndAsync.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

StringReader enables you to read a string synchronously or asynchronously. You can read a character at a time with the Read or the ReadAsync method, a line at a time using the ReadLine or the ReadLineAsync method and an entire string using the ReadToEnd or the ReadToEndAsync method.

Important noteImportant

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

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.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

FileAppendText

FileInfoAppendText

Get the size of a file.

FileInfoLength

Get the attributes of a file.

FileGetAttributes

Set the attributes of a file.

FileSetAttributes

Determine if a file exists.

FileExists

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Examples

The following example shows how to read an entire string asynchronously.

Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        ReadCharacters()
    End Sub

    Async Sub ReadCharacters()
        Dim stringToRead = New StringBuilder()
        stringToRead.AppendLine("Characters in 1st line to read")
        stringToRead.AppendLine("and 2nd line")
        stringToRead.AppendLine("and the end")

        Using reader As StringReader = New StringReader(stringToRead.ToString())
            Dim readText As String = Await reader.ReadToEndAsync()
            Console.WriteLine(readText)
        End Using 
    End Sub 
End Module 
' The example displays the following output: 
' 
' Characters in 1st line to read 
' and 2nd line 
' and the end 
'
using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            StringBuilder stringToRead = new StringBuilder();
            stringToRead.AppendLine("Characters in 1st line to read");
            stringToRead.AppendLine("and 2nd line");
            stringToRead.AppendLine("and the end");

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                string readText = await reader.ReadToEndAsync();
                Console.WriteLine(readText);
            }
        }
    }
}
// The example displays the following output: 
// 
// Characters in 1st line to read 
// and 2nd line 
// and the end 
//
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

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

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

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.