将字符串数据写入MemoryMappedFile [英] Write string data to MemoryMappedFile

查看:55
本文介绍了将字符串数据写入MemoryMappedFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此处

我很难弄清楚如何获取字符串"THIS IS A TEST MESSAGE"以存储在内存映射文件中,然后再将其拉出另一端.本教程说要使用字节数组.请原谅我是新手,请先尝试一下.

I am having a hard time figuring out how to get a string "THIS IS A TEST MESSAGE" to store in the memory mapped file and then pull it out on the other side. The tutorial says to use byte array. Forgive me I am new to this and trying on my own first.

谢谢,凯文

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

推荐答案

您可以考虑编写字符串的长度,然后编写字符串的byte []形式.
例如,如果我想编写"Hello",则将其转换为字节:

You can consider writing the length of the string and then the byte[] form of your string.
For example if I would like to write "Hello" then I convert it into bytes:

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

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

then do following while writing into the 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);

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

While reading first go to position 54 and read 2 bytes holding length of your string. Then you can read an array of that length and convert it into 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));

这篇关于将字符串数据写入MemoryMappedFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆