using (MemoryMappedFile mmFile = MemoryMappedFile.CreateNew("Raise", 1000))
{
using (MemoryMappedViewAccessor accessor = mmFile.CreateViewAccessor())
{
accessor.Write(500, 11082003);
Console.WriteLine("Memory-mapped file created!");
}
}
using (MemoryMappedFile mmFile = MemoryMappedFile.OpenExisting("Raise"))
{
using (MemoryMappedViewAccessor accessor = mmFile.CreateViewAccessor())
{
int value = accessor.ReadInt32(500);
Console.WriteLine("The answer is : {0}", value);
}
}
But, every time I try to read my integer value of 11082003, this is what I get, System.IO.FileNotFoundException: 'Unable to find the specified file.'. What might be the issue?
You're disposing of the resource you've written to, which is only in memory. From the documentation for
MemoryMappedFile.CreateNew:So when you dispose of that, the name isn't valid any more.
If you change your code to avoid disposing of the "creating" part until the end, it works:
Alternatively, to use an actual file, you can use
CreateFromFileboth times: