Skip to main content
.NET Framework Class Library
StringReader Class

Implements a TextReader that reads from a string.

Namespace:   System.IO
Assemblies:   mscorlib (in mscorlib.dll)
  System.IO (in System.IO.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
public class StringReader extends [%$TOPIC/fzy4xe55_en-us_VS_110_3_0_4_0_0%]

The StringReader type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library 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 DisposeReleases all resources used by the TextReader object. (Inherited from TextReader.)
Protected method Supported by the XNA Framework Supported by Portable Class Library 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 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 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 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 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 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 PeekReturns the next available character but does not consume it. (Overrides TextReaderPeek.)
Public method Supported by the XNA Framework Supported by Portable Class Library 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 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 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 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 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 ReadLineReads a line of characters from the current string and returns the data as a string. (Overrides TextReaderReadLine.)
Public method 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 ReadToEndReads all characters from the current position to the end of the string and returns them as a single string. (Overrides TextReaderReadToEnd.)
Public method 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 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.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

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

Portable Class Library

Supported in: Portable Class Library
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.