2007-12-17

Read a Stream into a String in C# .Net

using (MemoryStream ms = new MemoryStream())
{
//
// Write stuff into the memory stream with ms.Write();
//

// Jump to the start position of the stream
ms.Seek(0, SeekOrigin.Begin);

StreamReader rdr = new StreamReader(ms);
string str = rdr.ReadToEnd();
}

3 comments:

Anonymous said...

Great, straight to the point :)

Anonymous said...

Thanks. Setting the pointer to the beginning of the stream helped me in writing the stream into a string.

Anonymous said...

Straightforward and helpful. Thank you from a Chicago developer.