将字符串数据写入内存映射文件

6

我正在按照这篇教程学习。

我很难理解如何将字符串"THIS IS A TEST MESSAGE"存储在内存映射文件中,并在另一端取出它。教程中建议使用字节数组。请原谅,我是新手,首先尝试自己操作。

谢谢, 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();
    }
}

你能成功地在500位置写入和读取Int32型变量“42”吗?如果是这样,请将同样的概念扩展到字节数组:http://www.dotnetperls.com/convert-string-byte-array - paulsm4
我认为我已经成功地处理了字符串的写入。将测试并重新发布代码更改。 - kevp
我想要做的是让一个应用程序写入共享内存,然后关闭并读取消息。当两者同时运行时,这个例子似乎可以工作。 - kevp
在“经典Win32”中,共享内存对象应该持久存在,直到所有对该对象的引用都被删除(即使在第一个进程关闭之后)。据我所知,在Windows下运行的.Net程序也是如此。 - paulsm4
3个回答

14

你可以考虑先写出字符串的长度,然后再写出其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个字节,这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));

您,先生,是那些没有时间深入思考问题的人们的救星。 - Tanveer Badar
在读取时应该这样: MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("test"); - skoley

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));

1

我用这个来写一个字符串的字符:

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

这段文字使用了宽字符。C#和C++程序都可以读取它作为宽字符的数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接