using System.IO.MemoryMappedFiles; //create a memory-mapped file with a capacity of 1024 bytes using (var mmf = MemoryMappedFile.CreateNew("myMmf", 1024)) { //write data to the memory-mapped file using (var accessor = mmf.CreateViewAccessor()) { accessor.Write(0, (byte)65); accessor.Write(1, (byte)66); accessor.Write(2, (byte)67); } }
using System.IO.MemoryMappedFiles; //create a memory-mapped file with a capacity of 4096 bytes using (var mmf = MemoryMappedFile.CreateNew("myMmf", 4096)) { //read and write data to the memory-mapped file using (var stream = mmf.CreateViewStream()) { byte[] buffer = new byte[4] { 68, 69, 70, 71 }; stream.Write(buffer, 0, buffer.Length); stream.Seek(0, SeekOrigin.Begin); stream.Read(buffer, 0, buffer.Length); //buffer should now contain { 68, 69, 70, 71 } } }In both examples, we are creating a memory-mapped file with a specified name and capacity using the CreateNew method. In the first example, we are using the CreateViewAccessor method to write data to the memory-mapped file. In the second example, we are using the CreateViewStream method to read and write data to the memory-mapped file. This method belongs to the System.IO.MemoryMappedFiles package library.