# C# 文件映射内存共享

代码

Global\ 前缀是为了全局共享,比如 windows 服务程序 和一般程序进行共享时,不加全局前缀无法共享

C#
byte[] bytes = new byte[1024];

MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("Global\\key", bytes.Length);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.WriteArray(0, bytes, 0, bytes.Length);

MemoryMappedFile mmf1 = MemoryMappedFile.CreateOrOpen("Global\\key", bytes.Length);
MemoryMappedViewAccessor accessor1 = mmf1.CreateViewAccessor();
accessor1.ReadArray(0, bytes, 0, bytes.Length);

//当所有对象释放后,内存失效