Reading a String from a Stream

07.02.10

Problem: You need to read some text from a stream of some sort.

Solution: Use StreamReader class , set the postion to 0 and call ReadToEnd().

Code!

 

static string GetStringFromStream(Stream inputStream)
{
var pos = inputStream.Position;
var reader = new StreamReader(inputStream);
inputStream.Position = 0;
var text = reader.ReadToEnd();
inputStream.Position = pos;
return text;
}