StreamReader.ReadToEnd 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
读取来自流的当前位置到结尾的所有字符。
public:
 override System::String ^ ReadToEnd();public override string ReadToEnd ();override this.ReadToEnd : unit -> stringPublic Overrides Function ReadToEnd () As String返回
字符串形式的流的其余部分(从当前位置到结尾)。 如果当前位置位于流结尾,则返回空字符串 (“”)。
例外
没有足够的内存来为返回的字符串分配缓冲区。
出现 I/O 错误。
示例
下面的代码示例在一个操作中一直读取到文件末尾。
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
      {
         //This allows you to do one Read operation.
         Console::WriteLine( sr->ReadToEnd() );
      }
      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))
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
Imports System.IO
Imports System.Text
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)
            'This allows you to do one Read operation.
            Console.WriteLine(sr.ReadToEnd())
            sr.Close()
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
注解
此方法重写 TextReader.ReadToEnd。
ReadToEnd 需要读取从当前位置到流末尾的所有输入时,效果最佳。 如果需要对从流读取的字符数进行更多控制,请使用 Read(Char[], Int32, Int32) 方法重载,这通常会提高性能。
ReadToEnd 假定流知道何时到达终点。 对于交互式协议,服务器仅在你请求数据且未关闭连接时发送数据, ReadToEnd 可能会无限期阻止,因为它不会到达终点,因此应避免使用。
请注意,使用 Read 方法时,使用与流内部缓冲区大小相同的缓冲区会更有效。 如果在构造流时未指定缓冲区的大小,则其默认大小为 4 KB (4096 字节) 。
如果当前方法引发 , OutOfMemoryException则读取器在基础 Stream 对象中的位置将增加方法能够读取的字符数,但已读入内部 ReadLine 缓冲区的字符将被丢弃。 如果在将数据读入缓冲区后操作基础流的位置,则基础流的位置可能与内部缓冲区的位置不匹配。 若要重置内部缓冲区,请 DiscardBufferedData 调用 方法;但是,此方法会降低性能,并且仅应在绝对必要时调用。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。