using System.IO; // create a memory stream and write some data to it MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.WriteLine("Hello"); writer.WriteLine("World"); // flush the memory stream and write its contents to the console output stream.Flush(); stream.Position = 0; StreamReader reader = new StreamReader(stream); Console.WriteLine(reader.ReadToEnd());
using System.IO; // create a memory stream and write some data to it MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.WriteLine("Hello"); writer.WriteLine("World"); // flush the memory stream and write its contents to a file stream.Flush(); stream.Position = 0; FileStream file = new FileStream("output.txt", FileMode.Create); stream.CopyTo(file); file.Close();In this example, we create a memory stream, write some data to it, flush it, and then write its contents to a file using the CopyTo method. The System.IO MemoryStream class and its methods are part of the .NET Framework class library.