ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

ReadToEnd

Returns a string with the content of the input stream

Source

/// <summary>
/// Reads the content of the stream
/// </summary>
/// <param name="str">Original stream</param>
/// <returns>Returns a string with the content of the input stream</returns>
public static String ReadToEnd(this Stream str)
{
   try { str.Position = 0; }
   catch { }
   using (StreamReader sr = new StreamReader(str))
   {
      return sr.ReadToEnd();
   }
}

Example

//This is only an example. A file could be easily read to a String by using "System.IO.File.ReadAllText"

using (System.IO.FileStream fs = new System.IO.FileStream(@"c:\test.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    String StreamContent = fs.ReadToEnd();
}

Author: Jonnidip

Submitted on: 12 nov 2009

Language: C#

Type: System.IO.Stream

Views: 4628