例一:
1.写入文件-----创建的项目:控制台应用(.NET Framework)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace Shared_Memory1
{
class Program
{
static void Main(string[] args)
{
long capacity = 1 << 10 << 10;
//创建或者打开共享内存
using (var mmf = MemoryMappedFile.CreateOrOpen("testMmf", capacity, MemoryMappedFileAccess.ReadWrite))
{
//通过MemoryMappedFile的CreateViewAccssor方法获得共享内存的访问器
var viewAccessor = mmf.CreateViewAccessor(0, capacity);
//循环写入,使在这个进程中可以向共享内存中写入不同的字符串值
while (true)
{
Console.WriteLine("请输入一行要写入共享内存的文字:");
string input = Console.ReadLine();
//向共享内存开始位置写入字符串的长度
viewAccessor.Write(0, input.Length);
//向共享内存4位置写入字符
viewAccessor.WriteArray<char>(4, input.ToArray(), 0, input.Length);
}
}
}
}
}
2.读取文件-----创建的项目:Windows 窗体应用(.NET Framework)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.MemoryMappedFiles;
using System.Threading;
namespace Shared_Memory2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
long capacity = 88888;
using (var mmf = MemoryMappedFile.OpenExisting("testMmf"))
{
MemoryMappedViewAccessor viewAccessor = mmf.CreateViewAccessor(0, capacity);
//循环刷新共享内存字符串的值
//while (true)
//{
//读取字符长度
int strLength = viewAccessor.ReadInt32(0);
char[] charsInMMf = new char[strLength];
//读取字符
viewAccessor.ReadArray<char>(4, charsInMMf, 0, strLength);
textBox1.Text = new string(charsInMMf);
Thread.Sleep(200);
//}
}
}
}
}
例二
1.写入文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WriteRead
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button3_Click(object sender, EventArgs e)
{
MemoryMappedFile memory = MemoryMappedFile.CreateOrOpen("testMmf", 1024); // 创建指定大小的内存文件,会在应用程序退出时自动释放
MemoryMappedViewAccessor accessor1 = memory.CreateViewAccessor(0,1024); // 访问内存文件对象
if(textBox1.Text=="")
{
MessageBox.Show("请输入值","经度", MessageBoxButtons.OK);
}
else
{
Double value = Convert.ToDouble(textBox1.Text);
accessor1.Write(4, value); // 在指定位置写入double值
/*
TextBox[] txts = new TextBox[3]; //创建控件数组(文本框数组)
txts[0] = this.textBox1;
txts[1] = this.textBox2;
txts[2] = this.textBox3;
double[] arr = new double[3]; //创建用于存储文本框中数据的数组arr[]
for (int i = 0; i <= 2; i++) //将文本框数据传入数据数组arr[]中
{
arr[i] = Convert.ToDouble(txts[i].Text);
}
accessor1.WriteArray<double>(4, arr, 0, arr.Length);
*/
}
accessor1.Dispose(); // 释放资源
}
}
}
2.读取文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;
namespace Shared_Memory3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
MemoryMappedFile memory = MemoryMappedFile.OpenExisting("testMmf"); // 获取指定名称内存文件
MemoryMappedViewAccessor accessor1 = memory.CreateViewAccessor(); // 访问内存文件对象
Double value = accessor1.ReadDouble(4); // 读取指定位置的数据
textBox1.Text = Convert.ToString(value);
accessor1.Dispose();
Thread.Sleep(200);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
参考:
https://blog.youkuaiyun.com/scimence/article/details/79417331
https://www.cnblogs.com/flyant/p/4443187.html
https://blog.youkuaiyun.com/changblade/article/details/82027440
http://c.biancheng.net/view/2928.html