using System.IO.MemoryMappedFiles; long fileSize = 1024; // 1 KB string mapName = "MyMemoryMap"; MemoryMappedFile memoryMap = MemoryMappedFile.CreateNew(mapName, fileSize);
using System.IO.MemoryMappedFiles; string mapName = "MyMemoryMap"; MemoryMappedFile memoryMap = MemoryMappedFile.OpenExisting(mapName);
using System.IO.MemoryMappedFiles; using System.Text; string mapName = "MyMemoryMap"; using (MemoryMappedFile memoryMap = MemoryMappedFile.OpenExisting(mapName)) using (MemoryMappedViewAccessor accessor = memoryMap.CreateViewAccessor()) { string message = "Hello, world!"; byte[] messageBytes = Encoding.ASCII.GetBytes(message); accessor.WriteArray(0, messageBytes, 0, messageBytes.Length); byte[] buffer = new byte[messageBytes.Length]; accessor.ReadArray(0, buffer, 0, messageBytes.Length); string readMessage = Encoding.ASCII.GetString(buffer); Console.WriteLine(readMessage); }This code opens an existing memory mapped file with the name "MyMemoryMap", writes the string "Hello, world!" to it, and then reads it back into a buffer, converting it back to a string and printing it to the console.