5

我在这里关注本教程

我很难弄清楚如何将字符串“这是一个测试消息”存储在内存映射文件中,然后将其从另一侧拉出。本教程说使用字节数组。原谅我,我是新手,先自己尝试。

谢谢,凯文

##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();
    }
}
4

3 回答 3

12

您可以考虑写入字符串的长度,然后写入字符串的 byte[] 形式。
例如,如果我想写“Hello”,那么我将其转换为字节:

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes("Hello");

然后在写入内存映射文件时执行以下操作。

MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(54, (ushort)Buffer.Length);
accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length);

读取时首先转到位置 54 并读取包含字符串长度的 2 个字节。然后您可以读取该长度的数组并将其转换为字符串。

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));
于 2012-10-18T18:07:17.300 回答
1

我用它来写字符串的字符:

string contentString = "Hello";
char[] charsToWrite = contentString.ToCharArray();
accessor.WriteArray(0, charsToWrite, 0, charsToWrite.Length);

这写了宽字符。C# 和 C++ 程序都能够将数据读取为宽字符。

于 2019-08-26T18:05:15.923 回答
1

它适用于 CreateOrOpen 而不是 CreateNew!使用相同的代码

MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("test", 1000);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
accessor.Write(54, (ushort)Buffer.Length);
accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length);

MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("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));
于 2020-04-09T12:57:55.450 回答