using System.IO.MemoryMappedFiles; // Open an existing memory mapped file using (var mmf = MemoryMappedFile.OpenExisting("MyMemoryMappedFile")) { // Create a view accessor for the file using (var accessor = mmf.CreateViewAccessor()) { // Write a string to the file string message = "Hello from the other process!"; byte[] buffer = Encoding.UTF8.GetBytes(message); accessor.WriteArray(0, buffer, 0, buffer.Length); } }
using System.IO.MemoryMappedFiles; // Open an existing memory mapped file using (var mmf = MemoryMappedFile.OpenExisting("MyMemoryMappedFile")) { // Create a view accessor for the file using (var accessor = mmf.CreateViewAccessor()) { // Read a string from the file byte[] buffer = new byte[256]; accessor.ReadArray(0, buffer, 0, buffer.Length); string message = Encoding.UTF8.GetString(buffer); Console.WriteLine(message); } }Both examples use the CreateViewAccessor method to create a view accessor for an existing memory mapped file. The accessor is used to write a string to the file in example 1, and read a string from the file in example 2. The MemoryMappedFile class is part of the System.IO.MemoryMappedFiles package library.