Brian Brian - 2 months ago 21 Question

How do you get a string from a MemoryStream?

If I am given a

that I know has been populated with a
, how do I get a
back out?


This sample shows how to read and write a string to a MemoryStream.

static void Main(string[] args)
    using (var ms = new MemoryStream())
        var sw = new StreamWriter(ms);
        sw.WriteLine("Hello World");
        // The string is currently stored in the 
        // StreamWriters buffer. Flushing the stream will 
        // force the string into the MemoryStream.

        // If we dispose the StreamWriter now, it will close 
        // the BaseStream (which is our MemoryStream) which 
        // will prevent us from reading from our MemoryStream
        //DON'T DO THIS - sw.Dispose();

        // The StreamReader will read from the current 
        // position of the MemoryStream which is currently 
        // set at the end of the string we just wrote to it. 
        // We need to set the position to 0 in order to read 
        // from the beginning.
        ms.Position = 0;
        var sr = new StreamReader(ms);
        var myStr = sr.ReadToEnd();

    Console.WriteLine("Press any key to continue.");