C#-MemoryMappedFile的使用可以同時讀寫
阿新 • • 發佈:2019-01-05
string fileName = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
using (var mmFile = MemoryMappedFile.CreateFromFile(fileName,FileMode.Create,"fileHandle",1024*1024)
{
string valueToWrite = "Written to the mapped memory file on" + DateTime.Now.ToString();
var myAccessor = mmFile.CreateViewAccessor();
myAccessor.WriteArray<byte>(0, Encoding.ASCII.GetBytes(valueToWrite),0, valueToWrite.Length);
var readout = new byte[valueToWrite.Length];
myAccessor.ReadArray<byte>(0, readout, 0, readout.Length);
var finalValue = Encoding.ASCII.GetString(readout);
MessageBox.Show(finalValue);
}