using System.IO; namespace SampleApp { class Program { static void Main(string[] args) { // Create a new memory stream var memStream = new MemoryStream(); // Write some data to the memory stream byte[] data = Encoding.ASCII.GetBytes("Hello World!"); memStream.Write(data, 0, data.Length); // Create a new file stream using (var fileStream = new FileStream("output.txt", FileMode.Create)) { // Write the entire memory stream to the file memStream.WriteTo(fileStream); } } } }
using System.IO; namespace SampleApp { class Program { static void Main(string[] args) { // Create a new memory stream var memStream = new MemoryStream(); // Write some data to the memory stream byte[] data = Encoding.ASCII.GetBytes("Hello World!"); memStream.Write(data, 0, data.Length); // Create a byte array to store the memory stream contents var byteArray = new byte[memStream.Length]; // Write the entire memory stream to the byte array memStream.WriteTo(new MemoryStream(byteArray)); // Display the contents of the byte array Console.WriteLine(Encoding.ASCII.GetString(byteArray)); } } }This example demonstrates how to write the contents of a memory stream to a byte array. The program first creates a new memory stream and writes some data to it. It then creates a byte array to store the memory stream contents, creates a new memory stream backed by the byte array, and writes the entire memory stream to the byte array using the WriteTo method. Finally, the program displays the contents of the byte array. Package Library: System.Runtime.