Schreiben Sie Zeichenfolgendaten in MemoryMappedFile

Ich folge diesem TutorialHier

Es fällt mir schwer, herauszufinden, wie ich eine Zeichenfolge "DIES IST EINE TESTMELDUNG" in der Speicherzuordnungsdatei speichern und dann auf der anderen Seite herausziehen kann. Das Tutorial sagt, Byte-Array zu verwenden. Vergib mir, ich bin neu in diesem Bereich und versuche es zuerst alleine.

Danke, Kevin

##Write to mapped file

using System;
using System.IO.MemoryMappedFiles;

class Program1
{
    static void Main()
    {
        // create a memory-mapped file of length 1000 bytes and give it a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
        // write an integer value of 42 to this file at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        accessor.Write(500, 42);
        Console.WriteLine("Memory-mapped file created!");
        Console.ReadLine(); // pause till enter key is pressed
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}   


##read from mapped file  
using System;
using System.IO.MemoryMappedFiles;
class Program2
{
    static void Main()
    {
        // open the memory-mapped with a 'map name' of 'test'
        MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test");
        // read the integer value at position 500
        MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
        int value = accessor.ReadInt32(500);
        // print it to the console
        Console.WriteLine("The answer is {0}", value);
        // dispose of the memory-mapped file object and its accessor
        accessor.Dispose();
        mmf.Dispose();
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage