You need to make sure that the mapName
argument (i.e. the third argument in your call to CreateFromFile
) is not identical to the file path. It will throw a PathNotFound
exception if you do. Not really helpful in figuring out why it is failing, I agree.
So your options for choosing a map name value:
- Generate some unique key, e.g.
Guid.NewGuid().ToString()
- Use a constant value, e.g. "MySpecialMapForThings"
- Use some convention, e.g. generate a unique key that you also use for just the file name part of the mapped file.
An example for the last option:
public static Tuple<FileInfo, string> GenerateMapInfo(string mapDirectory, string fileExtension)
{
var uniqueMapName = Guid.NewGuid().ToString();
var fileName = Path.Combine(mapDirectory, Path.ChangeExtension(uniqueMapName, fileExtension));
return Tuple.Create(new FileInfo(fileName), uniqueMapName);
}
public void WriteToFile(Tuple<FileInfo, string> mapInfo, string value)
{
byte[] newValue = Encoding.UTF8.GetBytes(value);
long capacity = newValue.Length + INT_MAXVALUE_TO_BYTEARRAY_LENGTH;
using (var mmf = MemoryMappedFile.CreateFromFile(mapInfo.Item1.FullName, FileMode.Create, mapInfo.Item2, capacity))
using (var accesor = mmf.CreateViewAccessor())
{
byte[] newValueLength = BitConverter.GetBytes(value.Length);
accesor.WriteArray(0, newValueLength, 0, newValueLength.Length);
accesor.WriteArray(INT_MAXVALUE_TO_BYTEARRAY_LENGTH, newValue, 0, newValue.Length);
}
}