You might consider writing the length of the string, and then the byte [] of your string.
For example, if I would like to write "Hello", then I will convert it to bytes:
byte[] Buffer = ASCIIEncoding.ASCII.GetBytes("Hello");
then do the following when writing to a memory mapped file.
MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000); MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(); accessor.Write(54, (ushort)Buffer.Length); accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length);
On first reading, go to position 54 and read 2 bytes while holding the length of your string. Then you can read an array of this length and convert it to a string.
MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000); MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(); ushort Size = accessor.ReadUInt16(54); byte[] Buffer = new byte[Size]; accessor.ReadArray(54 + 2, Buffer, 0, Buffer.Length); MessageBox.Show(ASCIIEncoding.ASCII.GetString(Buffer));
Demir source share