byte[] data = Encoding.ASCII.GetBytes("Hello,"); MemoryStream stream = new MemoryStream(); stream.Write(data, 0, data.Length); // write "Hello," to the stream byte[] newData = Encoding.ASCII.GetBytes(" world!"); stream.Append(newData, 0, newData.Length); // append " world!" to the stream Console.WriteLine(Encoding.ASCII.GetString(stream.ToArray())); // output: "Hello, world!"
using System.IO.MemoryMappedFiles; MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MyFile", 1024); // create a new memory mapped file MemoryMappedViewStream stream = mmf.CreateViewStream(); // get a view stream for the file byte[] data = new byte[] { 0x01, 0x02, 0x03 }; // some data to write stream.Write(data, 0, data.Length); // write the data to the stream byte[] newData = new byte[] { 0x04, 0x05 }; // some more data to append stream.Seek(0, SeekOrigin.End); // move the stream position to the end stream.Append(newData, 0, newData.Length); // append the new dataIn this example, we're using the MemoryMappedFile package library to create a new memory mapped file. We then get a view stream for the file, and write some data to it using the Write method. Finally, we append some more data to the stream using the Append method after moving the stream position to the end.