日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#内存映射文件学习总结

發布時間:2025/4/14 C# 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#内存映射文件学习总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#內存映射文件學習

http://www.cnblogs.com/flyant/p/4443187.html


內存映射文件是由一個文件到進程地址空間的映射。


? ? ? ? C#提供了允許應用程序把文件映射到一個進程的函(MemoryMappedFile.CreateOrOpen)。內存映射文件與虛擬內存有些類似,通過內存映射文件可以保留一個地址空間的區域,同時將物理存儲器提交給此區域,只是內存文件映射的物理存儲器來自一個已經存在于磁盤上的文件,而非系統的頁文件,而且在對該文件進行操作之前必須首先對文件進行映射,就如同將整個文件從磁盤加載到內存。由此可以看出,使用內存映射文件處理存儲于磁盤上的文件時,將不必再對文件執行I/O操作,這意味著在對文件進行處理時將不必再為文件申請并分配緩存,所有的文件緩存操作均由系統直接管理,由于取消了將文件數據加載到內存、數據從內存到文件的回寫以及釋放內存塊等步驟,使得內存映射文件在處理大數據量的文件時能起到相當重要的作用。另外,實際工程中的系統往往需要在多個進程之間共享數據,如果數據量小,處理方法是靈活多變的,如果共享數據容量巨大,那么就需要借助于內存映射文件來進行。實際上,內存映射文件正是解決本地多個進程間數據共享的最有效方法。


? ? ? ? 共享內存是內存映射文件的一種特殊情況,內存映射的是一塊內存,而非磁盤上的文件。共享內存的主語是進程(Process),操作系統默認會給每一個進程分配一個內存空間,每一個進程只允許訪問操作系統分配給它的哪一段內存,而不能訪問其他進程的。而有時候需要在不同進程之間訪問同一段內存,怎么辦呢?操作系統給出了創建訪問共享內存的API,需要共享內存的進程可以通過這一組定義好的API來訪問多個進程之間共有的內存,各個進程訪問這一段內存就像訪問一個硬盤上的文件一樣。而.Net 4.0中引入了System.IO.MemoryMappedFiles命名空間,這個命名空間的類對windows 共享內存相關API做了封裝,使.Net程序員可以更方便的使用內存映射文件。


內存映射文件實現進程間通訊


? ? ? ? 內存映射文件是實現進程通訊的又一種方法,我們可以通過共享剪貼板、共享物理文件來實現進程間的數據共享,這里我們還可以通過內存映射文件來實現共享,這樣,文件內的數據就可以用內存讀/寫指令來訪問,而不是用ReadFile和WriteFile這樣的I/O系統函數,從而提高了文件存取速度。這種方式更加快捷高效,最適用于需要讀取文件并且對文件內包含的信息做語法分析的應用程序,如:對輸入文件進行語法分析的彩色語法編輯器,編譯器等。這種數據共享是讓兩個或多個進程映射同一文件映射對象的視圖,即它們在共享同一物理存儲頁。這樣,當一個進程向內存映射文件的一個視圖寫入數據時,其他的進程立即在自己的視圖中看到變化。


注意:


? ? ? ? 對文件映射對象要使用同一名字。


? ? ? ? 是讓兩個或多個進程映射同一文件映射對象的視圖,即它們在共享同一物理存儲頁。這樣,當一個進程向內存映射文件的一個視圖寫入數據時,其他的進程立即在自己的視圖中看到變化。但要注意,對文件映射對象要使用同一名字。


?內存映射文件使用實例:


1. ? ? ?在同一進程內同時讀寫同一內存映射文件


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;


namespace UseMMFInProcess
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? CreateMemoryMapFile();
? ? ? ? }
? ? ? ? private const int FILE_SIZE = 512;
? ? ? ? /// <summary>
? ? ? ? /// 引用內存映射文件
? ? ? ? /// </summary>
? ? ? ? private MemoryMappedFile memoryFile = null;
? ? ? ? /// <summary>
? ? ? ? /// 用于訪問內存映射文件的存取對象
? ? ? ? /// </summary>
? ? ? ? private MemoryMappedViewAccessor accessor1, accessor2,accessor;
? ? ? ? /// <summary>
? ? ? ? /// 創建內存映射文件
? ? ? ? /// </summary>
? ? ? ? private void CreateMemoryMapFile()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? { ? ??
? ? ? ? ? ? ? ? memoryFile = MemoryMappedFile.CreateFromFile("MyFile.dat", FileMode.OpenOrCreate, "MyFile", FILE_SIZE); ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? //訪問文件前半段
? ? ? ? ? ? ? ? accessor1 = memoryFile.CreateViewAccessor(0, FILE_SIZE / 2); ? ? ? ? ? ?
? ? ? ? ? ? ? ? //訪問文件后半段
? ? ? ? ? ? ? ? accessor2 = memoryFile.CreateViewAccessor(FILE_SIZE / 2, FILE_SIZE / 2); ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? //訪問全部文件
? ? ? ? ? ? ? ? accessor = memoryFile.CreateViewAccessor();
? ? ? ? ? ? ? ? //InitFileContent();
? ? ? ? ? ? ? ? lblInfo.Text = "內存文件創建成功";
? ? ? ? ? ? ? ? ShowFileContents();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 關閉并釋放資源
? ? ? ? /// </summary>
? ? ? ? private void DisposeMemoryMapFile()
? ? ? ? {
? ? ? ? ? ? if (accessor1 != null)
? ? ? ? ? ? ? ? accessor1.Dispose();
? ? ? ? ? ? if (accessor2 != null)
? ? ? ? ? ? ? ? accessor2.Dispose();
? ? ? ? ? ? if (memoryFile != null)
? ? ? ? ? ? ? ? memoryFile.Dispose();
? ? ? ? }


? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? DisposeMemoryMapFile();
? ? ? ? }


? ? ? ? private void btnWrite1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (textBox1.Text.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = "請輸入一個字符";
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? char[] chs = textBox1.Text.ToCharArray();
? ? ? ? ? ? char ch = chs[0];
? ? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? ? ? accessor1.Write(i, ch);
? ? ? ? ? ??
? ? ? ? ? ? lblInfo.Text = "字符“" + ch + "”已寫到文件前半部份";
? ? ? ? ? ? ShowFileContents();
? ? ? ? }


? ? ? ? private void btnShow_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ShowFileContents();
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 初始化文件內容為可視的字符“0”
? ? ? ? /// </summary>
? ? ? ? private void InitFileContent()
? ? ? ? {
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE; i += 2)?
? ? ? ? ? ? ? ? accessor.Write(i,'0');
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 顯示文件內容
? ? ? ? /// </summary>
? ? ? ? private void ShowFileContents()
? ? ? ? {
? ? ? ? ? ? StringBuilder sb = new StringBuilder(FILE_SIZE);
? ? ? ? ? ? sb.Append("上半段內容:\n");


? ? ? ? ? ? int j = 0;
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sb.Append("\t");
? ? ? ? ? ? ? ? char ch = accessor.ReadChar(i);
? ? ? ? ? ? ? ? sb.Append(j);
? ? ? ? ? ? ? ? sb.Append(":");
? ? ? ? ? ? ? ? sb.Append(ch);
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? ? ? sb.Append("\n下半段內容:\n");


? ? ? ? ? ? for (int i = FILE_SIZE / 2; i < FILE_SIZE; i += 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sb.Append("\t");
? ? ? ? ? ? ? ? char ch = accessor.ReadChar(i);
? ? ? ? ? ? ? ? sb.Append(j);
? ? ? ? ? ? ? ? sb.Append(":");
? ? ? ? ? ? ? ? sb.Append(ch);
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? ? ? richTextBox1.Text = sb.ToString();
? ? ? ? }


? ? ? ? private void btnWrite2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (textBox2.Text.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = "請輸入一個字符";
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? char[] chs = textBox2.Text.ToCharArray();
? ? ? ? ? ? char ch = chs[0];


? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? ? ? accessor2.Write(i, ch);
? ? ? ? ? ? lblInfo.Text = "字符“" + ch + "”已寫到文件后半部份";
? ? ? ? ? ? ShowFileContents();
? ? ? ? }
? ? }
}


2. ? ? ?使用內存映射文件在進程間傳送值類型數據


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace UseMMFBetweenProcess
{
? ? /// <summary>
? ? /// 要共享的數據結構,注意,其成員不能是引用類型
? ? /// </summary>
? ? public struct MyStructure
? ? {
? ? ? ? public int IntValue
? ? ? ? {
? ? ? ? ? ? get;
? ? ? ? ? ? set;
? ? ? ? }
? ? ? ? public float FloatValue
? ? ? ? {
? ? ? ? ? ? get;
? ? ? ? ? ? set;
? ? ? ? }?
? ? }?
}


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.MemoryMappedFiles;
using System.IO;


namespace UseMMFBetweenProcess
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? InitMemoryMappedFile();
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 內存映射文件的容量
? ? ? ? /// </summary>
? ? ? ? private const int FileSize = 1024 * 1024;
? ? ? ? private MemoryMappedFile file = null;
? ? ? ? private MemoryMappedViewAccessor accessor = null;


? ? ? ? /// <summary>
? ? ? ? /// 初始化內存映射文件
? ? ? ? /// </summary>
? ? ? ? private void InitMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? file = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess", FileSize);
? ? ? ? ? ? accessor = file.CreateViewAccessor();
? ? ? ? ? ? lblInfo.Text = "內存文件創建或連接成功"; ? ? ? ??
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 要共享的數據對象
? ? ? ? /// </summary>
? ? ? ? private MyStructure data;


? ? ? ? /// <summary>
? ? ? ? /// 顯示數據到窗體上
? ? ? ? /// </summary>
? ? ? ? private void ShowData()
? ? ? ? {
? ? ? ? ? ? textBox1.Text = data.IntValue.ToString();
? ? ? ? ? ? textBox2.Text = data.FloatValue.ToString();
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 根據用戶輸入更新數據
? ? ? ? /// </summary>
? ? ? ? private void UpdateData()
? ? ? ? {
? ? ? ? ? ? data.IntValue = int.Parse(textBox1.Text);
? ? ? ? ? ? data.FloatValue = float.Parse(textBox2.Text);
? ? ? ? }


? ? ? ? private void btnSave_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UpdateData();
? ? ? ? ? ? ? ? accessor.Write<MyStructure>(0, ref data);
? ? ? ? ? ? ? ? lblInfo.Text = "數據已經保存到內存文件中";
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = ex.Message;
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? private void btnLoad_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? accessor.Read<MyStructure>(0, out data);
? ? ? ? ? ? ShowData();
? ? ? ? ? ? lblInfo.Text = "成功從內存文件中提取了數據";
? ? ? ? }


? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? if (accessor != null)
? ? ? ? ? ? ? ? accessor.Dispose();
? ? ? ? ? ? if (file != null)
? ? ? ? ? ? ? ? file.Dispose();
? ? ? ? }
? ? }
}


3. ? ? ?利用序列化技術通過內存映射文件實現進程通訊


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.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace UseMMFBetweenProcess2
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? InitMemoryMappedFile();
? ? ? ? }
? ? ? ?
? ? ? ? /// <summary>
? ? ? ? /// 圖片
? ? ? ? /// </summary>
? ? ? ? private Image bmp
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return pictureBox1.Image;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? pictureBox1.Image = value;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ?
? ? ? ?/// <summary>
? ? ? ?/// 圖片說明
? ? ? ?/// </summary>
? ? ? ? private string info
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return txtImageInfo.Text;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? txtImageInfo.Text = value;
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? private MemoryMappedFile memoryFile = null;


? ? ? ? private MemoryMappedViewStream stream = null;


? ? ? ? /// <summary>
? ? ? ? /// 最大容量:10M
? ? ? ? /// </summary>
? ? ? ? private const int FileSize = 1024 * 1024 * 10; ?


? ? ? ? /// <summary>
? ? ? ? /// 創建內存映射文件,獲取其讀寫流
? ? ? ? /// </summary>
? ? ? ? private void InitMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? memoryFile = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess2", FileSize);
? ? ? ? ? ? stream = memoryFile.CreateViewStream();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? ? ? ? Close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 釋放相關資源
? ? ? ? /// </summary>
? ? ? ? private void DisposeMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? if (stream != null)
? ? ? ? ? ? ? ? stream.Close();
? ? ? ? ? ? if (memoryFile != null)
? ? ? ? ? ? ? ? memoryFile.Dispose();
? ? ? ? }


? ? ? ? private void btnLoadPic_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ChooseImageFile();
? ? ? ? }


? ? ? ? //選擇圖片
? ? ? ? private void ChooseImageFile()
? ? ? ? {
? ? ? ? ? ? if (openFileDialog1.ShowDialog() == DialogResult.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bmp = new Bitmap(openFileDialog1.FileName);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //根據用戶設定的信息創建對象
? ? ? ? private MyPic CreateMyPicObj()
? ? ? ? {
? ? ? ? ? ? MyPic obj = new MyPic();
? ? ? ? ? ? obj.pic = bmp;
? ? ? ? ? ? obj.picInfo = info;
? ? ? ? ? ? return obj;
? ? ? ? }


? ? ? ? /// <summary>
? ? ? ? /// 將MyPic對象保存到內存映射文件中
? ? ? ? /// </summary>
? ? ? ? private void SaveToMMF()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? MyPic obj = CreateMyPicObj();
? ? ? ? ? ? IFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? stream.Seek(0, SeekOrigin.Begin);
? ? ? ? ? ? formatter.Serialize(stream, obj);
? ? ? ? ? ? MessageBox.Show("對象已保存到內存映射文件中");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? ? }
? ? ? ? }


?private void LoadFromMMF()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ?// CreateMyPicObj();
? ? ? ? ? ? IFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? stream.Seek(0, SeekOrigin.Begin);
? ? ? ? ? ? MyPic obj = ? formatter.Deserialize(stream) as MyPic;
? ? ? ? ? ? if (obj != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bmp = obj.pic;
? ? ? ? ? ? ? ? info = obj.picInfo;
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? catch (Exception ex)
? ? ? ? ? {
? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? }
? ? ? ? }


? ? ? ? private void btnExit_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Close();
? ? ? ? }


? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? DisposeMemoryMappedFile();
? ? ? ? }


? ? ? ? private void btnSaveToMMF_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? SaveToMMF();
? ? ? ? }


? ? ? ? private void btnLoadFromMMF_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? LoadFromMMF();
? ? ? ? }
? ? }
}
========

C# 用內存映射文件讀取大文件(.txt)

http://www.cnblogs.com/criedshy/archive/2010/06/13/1757826.html
? 網上有好多這類的文章,大部分都是用C/C++寫的,也有部分C#寫的,都思想都是一樣的,調用win32 API。


? 至于什么是內存映射文件,相信還是有好多人不知道是怎么一回事的,我也是偶然看window 核心編程了解到的。


? C# 讀取大文件的方法也是用的用StreamReader一次讀出來,再用MemoryStream放在內存,再用StreamReader一行行的讀出來,速度也挺快的,16M的文本大概也就8秒左右,算起來差不多算快了。不過還是不能滿足大文件(我沒測試)。


string content = string.Empty;


? ? ? ? ? ? ? using (StreamReader sr = new StreamReader(op.FileName))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? content = sr.ReadToEnd();//一次性讀入內存
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MemoryStream ms = new MemoryStream(Encoding.GetEncoding("GB2312").GetBytes(content));//放入內存流,以便逐行讀取
? ? ? ? ? ? ? ? long line = 0;
? ? ? ? ? ? ? ? using (StreamReader sr = new StreamReader(ms))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? while (sr.Peek() > -1)
? ? ? ? ? ? ? ? ? ? {


? ? ? ? ? ? ? ? ? ? ? ? this.richTextBox1.AppendText(sr.ReadLine() + "\r\n");
? ? ? ? ? ? ? ? ? ? ? ? Application.DoEvents();
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ?}


 內存映射文件概述


  內存文件映射也是Windows的一種內存管理方法,提供了一個統一的內存管理特征,使應用程序可以通過內存指針對磁盤上的文件進行訪問,其過程就如同對加載了文件的內存的訪問。通過文件映射這種使磁盤文件的全部或部分內容與進程虛擬地址空間的某個區域建立映射關聯的能力,可以直接對被映射的文件進行訪問,而不必執行文件I/O操作也無需對文件內容進行緩沖處理。內存文件映射的這種特性是非常適合于用來管理大尺寸文件的。


  在使用內存映射文件進行I/O處理時,系統對數據的傳輸按頁面來進行。至于內部的所有內存頁面則是由虛擬內存管理器來負責管理,由其來決定內存頁面何時被分頁到磁盤,哪些頁面應該被釋放以便為其它進程提供空閑空間,以及每個進程可以擁有超出實際分配物理內存之外的多少個頁面空間等等。由于虛擬內存管理器是以一種統一的方式來處理所有磁盤I/O的(以頁面為單位對內存數據進行讀寫),因此這種優化使其有能力以足夠快的速度來處理內存操作。


  使用內存映射文件時所進行的任何實際I/O交互都是在內存中進行并以標準的內存地址形式來訪問。磁盤的周期性分頁也是由操作系統在后臺隱蔽實現的,對應用程序而言是完全透明的。內存映射文件的這種特性在進行大文件的磁盤事務操作時將獲得很高的效益。


  需要說明的是,在系統的正常的分頁操作過程中,內存映射文件并非一成不變的,它將被定期更新。如果系統要使用的頁面目前正被某個內存映射文件所占用,系統將釋放此頁面,如果頁面數據尚未保存,系統將在釋放頁面之前自動完成頁面數據到磁盤的寫入。


  對于使用頁虛擬存儲管理的Windows操作系統,內存映射文件是其內部已有的內存管理組件的一個擴充。由可執行代碼頁面和數據頁面組成的應用程序可根據需要由操作系統來將這些頁面換進或換出內存。如果內存中的某個頁面不再需要,操作系統將撤消此頁面原擁用者對它的控制權,并釋放該頁面以供其它進程使用。只有在該頁面再次成為需求頁面時,才會從磁盤上的可執行文件重新讀入內存。同樣地,當一個進程初始化啟動時,內存的頁面將用來存儲該應用程序的靜態、動態數據,一旦對它們的操作被提交,這些頁面也將被備份至系統的頁面文件,這與可執行文件被用來備份執行代碼頁面的過程是很類似的。圖1展示了代碼頁面和數據頁面在磁盤存儲器上的備份過程:


圖1 進程的代碼頁、數據頁在磁盤存儲器上的備份


  顯然,如果可以采取同一種方式來處理代碼和數據頁面,無疑將會提高程序的執行效率,而內存映射文件的使用恰恰可以滿足此需求。
對大文件的管理


  內存映射文件對象在關閉對象之前并沒有必要撤銷內存映射文件的所有視圖。在對象被釋放之前,所有的臟頁面將自動寫入磁盤。通過 CloseHandle()關閉內存映射文件對象,只是釋放該對象,如果內存映射文件代表的是磁盤文件,那么還需要調用標準文件I/O函數來將其關閉。在處理大文件處理時,內存映射文件將表示出卓越的優勢,只需要消耗極少的物理資源,對系統的影響微乎其微。下面先給出內存映射文件的一般編程流程框圖:


圖2 使用內存映射文件的一般流程


  而在某些特殊行業,經常要面對十幾GB乃至幾十GB容量的巨型文件,而一個32位進程所擁有的虛擬地址空間只有232 = 4GB,顯然不能一次將文件映像全部映射進來。對于這種情況只能依次將大文件的各個部分映射到進程中的一個較小的地址空間。這需要對上面的一般流程進行適當的更改:


  1)映射文件開頭的映像。


  2)對該映像進行訪問。


  3)取消此映像


  4)映射一個從文件中的一個更深的位移開始的新映像。


  5)重復步驟2,直到訪問完全部的文件數據。




? ?下面是用C#寫的代碼,大部分代碼轉自網上,自己在原來的基礎上改了一改。


C#內存映射文件代碼
?
? ? ?經過測試16M的文本4秒可以讀出來。


? ? ?現在是有兩個問題還沒有解決:


1.就是編碼的問題,用Unicode解碼的時候,文件大會很慢,而用ANSI和ASCII就很快。不知道為什么,望知情者告之。


2.怎么知道文件的編碼是什么?用win32 IsTestUnicode只能判斷兩種,而且還不保證是對。
========

c#實現內存映射文件共享內存

http://blog.csdn.net/wangtiewei/article/details/51112668 ?
內存映射文件是利用虛擬內存把文件映射到進程的地址空間中去,在此之后進程操作文件,就像操作進程空間里的地址一樣了,比如使用C語言的 memcpy等內存操作的函數。這種方法能夠很好的應用在需要頻繁處理一個文件或者是一個大文件的場合,這種方式處理IO效率比普通IO效率要高
共享內存是內存映射文件的一種特殊情況,內存映射的是一塊內存,而非磁盤上的文件。共享內存的主語是進程(Process),操作系統默認會給每一 個進程分配一個內存空間,每一個進程只允許訪問操作系統分配給它的哪一段內存,而不能訪問其他進程的。而有時候需要在不同進程之間訪問同一段內存,怎么辦 呢?操作系統給出了創建訪問共享內存的API,需要共享內存的進程可以通過這一組定義好的API來訪問多個進程之間共有的內存,各個進程訪問這一段內存就 像訪問一個硬盤上的文件一樣。而.Net 4.0中引入了System.IO. MemoryMappedFiles命名空間,這個命名空間的類對windows 共享內存相關API做了封裝,使.Net程序員可以更方便的使用內存映射文件。
在C#中使用共享內存。以下App1的代碼讓用戶輸入一行文本到共享內存中;App2不停的刷新控制臺,輸出最新的共享內存內容;App3實現的功能和App2相同,但讀取方法不同。


App1代碼: ?
using System; ?
using System.Collections.Generic;android從資源文件中讀取文件流顯示 ?
using System.Linq; ?
using System.Text; ?
??
using System.IO; ?
??
//引用內存映射文件命名空間 ?
using System.IO.MemoryMappedFiles; ?
??
namespace App1 ?
{ ?
? ? 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); ?
? ? ? ? ? ? ? ? } ?
??
? ? ? ? ? ? } ?
? ? ? ? ? ? ??
? ? ? ? } ?
? ? } ?
} ?


App2代碼: ?
using System; ?
using System.Collections.Generic; ?
using System.Linq; ?
using System.Text; ?
using System.Threading; ?
??
//引用使用內存映射文件需要的命名空間 ?
using System.IO.MemoryMappedFiles; ?
??
namespace App2 ?
{ ?
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? ? long capacity = 1<<10<<10; ?
??
? ? ? ? ? ? ? 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); ?
? ? ? ? ? ? ? ? ? ? ? Console.Clear(); ?
? ? ? ? ? ? ? ? ? ? ? Console.Write(charsInMMf); ?
? ? ? ? ? ? ? ? ? ? ? Console.Write("\r"); ?
? ? ? ? ? ? ? ? ? ? ? Thread.Sleep(200); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? } ?
? ? ? ? } ?
? ? } ?
} ?


App3代碼: ?
using System; ?
using System.Collections.Generic; ?
using System.Linq; ?
using System.Text; ?
??
using System.IO.MemoryMappedFiles; ?
using System.IO; ?
??
namespace App3 ?
{ ?
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? long capacity = 1 << 10 << 10; ?
? ? ? ? ? ? //打開共享內存 ?
? ? ? ? ? ? using (var mmf = MemoryMappedFile.OpenExisting("testMmf")) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //使用CreateViewStream方法返回stream實例 ?
? ? ? ? ? ? ? ? using (var mmViewStream = mmf.CreateViewStream(0, capacity)) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? //這里要制定Unicode編碼否則會出問題 ?
? ? ? ? ? ? ? ? ? ? using (BinaryReader rdr = new BinaryReader(mmViewStream,Encoding.Unicode)) ?
? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? while (true) ?
? ? ? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? mmViewStream.Seek(0, SeekOrigin.Begin); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? int length = rdr.ReadInt32(); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? char[] chars = rdr.ReadChars(length); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Write(chars); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Write("\r"); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Threading.Thread.Sleep(200); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Clear(); ?
? ? ? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? } ?
} ?
在讀數據時用了2種方法。
因為在之前很少會用到進程之間的通信,所以此方法只是想初步的認識下。此程序寫的過于簡陋,有很多東西都沒有去判斷。比如說是怎么創建了一個共享內存怎么取刪除它等等。。。
========

用C#實現的內存映射

http://blog.csdn.net/linux7985/article/details/5853358


當文件過大時,無法一次性載入內存時,就需要分次,分段的載入文件


主要是用了以下的WinAPI


LPVOID MapViewOfFile(HANDLE hFileMappingObject,
  DWORD dwDesiredAccess,
  DWORD dwFileOffsetHigh,
  DWORD dwFileOffsetLow,
  DWORD dwNumberOfBytesToMap);
?
  MapViewOfFile() 函數負責把文件數據映射到進程的地址空間,參數hFileMappingObject 為 CreateFileMapping()返回的文件映像對象句柄。參數dwDesiredAccess則再次指定了對文件數據的訪問方式,而且同樣要與 CreateFileMapping()函數所設置的保護屬性相匹配。雖然這里一再對保護屬性進行重復設置看似多余,但卻可以使應用程序能更多的對數據的保護屬性實行有效控制。MapViewOfFile()函數允許全部或部分映射文件,在映射時,需要指定數據文件的偏移地址以及待映射的長度。其中,文件的偏移地址由DWORD型的參數dwFileOffsetHigh和dwFileOffsetLow組成的64位值來指定,而且必須是操作系統的分配粒度的整數倍,對于Windows操作系統,分配粒度固定為64KB。當然,也可以通過如下代碼來動態獲取當前操作系統的分配粒度:


  SYSTEM_INFO sinf;
  GetSystemInfo(&sinf);
  DWORD dwAllocationGranularity = sinf.dwAllocationGranularity;


  參數dwNumberOfBytesToMap指定了數據文件的映射長度,這里需要特別指出的是,對于Windows 9x操作系統,如果MapViewOfFile()無法找到足夠大的區域來存放整個文件映射對象,將返回空值(NULL);但是在Windows 2000下,MapViewOfFile()只需要為必要的視圖找到足夠大的一個區域即可,而無須考慮整個文件映射對象的大小。


由此看出,分頁映射文件時,每頁的起始位置startpos,必須為64K的整數倍。


以下貼出源代碼,防止忘記了


using System; ?
using System.Collections.Generic; ?
using System.Text; ?
using System.Runtime.InteropServices; ?
??
namespace BlueVision.SaYuan.FileMapping ?
{ ?
? ? public class ShareMemory ?
? ? { ?
? ? ? ? [DllImport( "user32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr SendMessage( IntPtr hWnd, int Msg, int wParam, IntPtr lParam ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr CreateFileMapping( IntPtr hFile, IntPtr lpAttributes, uint flProtect, uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr OpenFileMapping( int dwDesiredAccess, [MarshalAs( UnmanagedType.Bool )] bool bInheritHandle, string lpName ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr MapViewOfFile( IntPtr hFileMapping, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern bool UnmapViewOfFile( IntPtr pvBaseAddress ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern bool CloseHandle( IntPtr handle ); ?
??
? ? ? ? [DllImport( "kernel32", EntryPoint = "GetLastError" )] ?
? ? ? ? public static extern int GetLastError(); ?
??
? ? ? ? [DllImport( "kernel32.dll" )] ?
? ? ? ? static extern void GetSystemInfo( out SYSTEM_INFO lpSystemInfo ); ?
??
? ? ? ? [StructLayout( LayoutKind.Sequential )] ?
? ? ? ? public struct SYSTEM_INFO ?
? ? ? ? { ?
? ? ? ? ? ? public ushort processorArchitecture; ?
? ? ? ? ? ? ushort reserved; ?
? ? ? ? ? ? public uint pageSize; ?
? ? ? ? ? ? public IntPtr minimumApplicationAddress; ?
? ? ? ? ? ? public IntPtr maximumApplicationAddress; ?
? ? ? ? ? ? public IntPtr activeProcessorMask; ?
? ? ? ? ? ? public uint numberOfProcessors; ?
? ? ? ? ? ? public uint processorType; ?
? ? ? ? ? ? public uint allocationGranularity; ?
? ? ? ? ? ? public ushort processorLevel; ?
? ? ? ? ? ? public ushort processorRevision; ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取系統的分配粒度 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public static uint GetPartitionsize() ?
? ? ? ? { ?
? ? ? ? ? ? SYSTEM_INFO sysInfo; ?
? ? ? ? ? ? GetSystemInfo( out sysInfo ); ?
? ? ? ? ? ? return sysInfo.allocationGranularity; ?
? ? ? ? } ?
??
? ? ? ? const int ERROR_ALREADY_EXISTS = 183; ?
??
? ? ? ? const int FILE_MAP_COPY = 0x0001; ?
? ? ? ? const int FILE_MAP_WRITE = 0x0002; ?
? ? ? ? const int FILE_MAP_READ = 0x0004; ?
? ? ? ? const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004; ?
??
? ? ? ? const int PAGE_READONLY = 0x02; ?
? ? ? ? const int PAGE_READWRITE = 0x04; ?
? ? ? ? const int PAGE_WRITECOPY = 0x08; ?
? ? ? ? const int PAGE_EXECUTE = 0x10; ?
? ? ? ? const int PAGE_EXECUTE_READ = 0x20; ?
? ? ? ? const int PAGE_EXECUTE_READWRITE = 0x40; ?
??
? ? ? ? const int SEC_COMMIT = 0x8000000; ?
? ? ? ? const int SEC_IMAGE = 0x1000000; ?
? ? ? ? const int SEC_NOCACHE = 0x10000000; ?
? ? ? ? const int SEC_RESERVE = 0x4000000; ?
??
? ? ? ? IntPtr m_fHandle; ?
??
? ? ? ? IntPtr m_hSharedMemoryFile = IntPtr.Zero; ?
? ? ? ? IntPtr m_pwData = IntPtr.Zero; ?
? ? ? ? bool m_bAlreadyExist = false; ?
? ? ? ? bool m_bInit = false; ?
? ? ? ? uint m_MemSize = 0x1400000;//20M ?
? ? ? ? long m_offsetBegin = 0; ?
? ? ? ? long m_FileSize = 0; ?
? ? ? ? FileReader File = new FileReader(); ?
? ??
? ? ? ? /// <summary> ?
? ? ? ? /// ?初始化文件 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="MemSize">緩沖大小</param> ?
? ? ? ? public ShareMemory( string filename, uint memSize ) ?
? ? ? ? { ?
? ? ? ? ? ? // 分頁映射文件時,每頁的起始位置startpos,必須為64K的整數倍。 ?
? ? ? ? ? ? // memSize即緩存區的大小必須是系統分配粒度的整倍說,window系統的分配粒度是64KB ?
? ? ? ? ? ? this.m_MemSize = memSize; ?
? ? ? ? ? ? Init( filename ); ?
? ? ? ? } ?
? ??
? ? ? ? /// <summary> ?
? ? ? ? /// 默認映射20M緩沖 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="filename"></param> ?
? ? ? ? public ShareMemory( string filename ) ?
? ? ? ? { ?
? ? ? ? ? ? this.m_MemSize = 0x1400000; ?
? ? ? ? ? ? Init( filename ); ?
? ? ? ? } ?
??
? ? ? ? ~ShareMemory() ?
? ? ? ? { ?
? ? ? ? ? ? Close(); ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 初始化共享內存 ?
? ? ? ? /// ??
? ? ? ? /// 共享內存名稱 ?
? ? ? ? /// 共享內存大小 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="strName"></param> ?
? ? ? ? protected void Init( string strName ) ?
? ? ? ? { ?
? ? ? ? ? ? //if (lngSize <= 0 || lngSize > 0x00800000) lngSize = 0x00800000; ?
??
? ? ? ? ? ? if ( !System.IO.File.Exists( strName ) ) throw new Exception( "未找到文件" ); ?
??
? ? ? ? ? ? System.IO.FileInfo f = new System.IO.FileInfo( strName ); ? ?
? ? ? ? ? ? m_FileSize = f.Length; ? ?
? ? ? ? ? ? m_fHandle = File.Open( strName ); ?
??
? ? ? ? ? ? if ( strName.Length > 0 ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //創建文件映射 ?
? ? ? ? ? ? ? ? m_hSharedMemoryFile = CreateFileMapping( m_fHandle, IntPtr.Zero, ( uint )PAGE_READONLY, 0, ( uint )m_FileSize, "mdata" ); ?
? ? ? ? ? ? ? ? if ( m_hSharedMemoryFile == IntPtr.Zero ) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? m_bAlreadyExist = false; ?
? ? ? ? ? ? ? ? ? ? m_bInit = false; ?
? ? ? ? ? ? ? ? ? ? throw new Exception( "CreateFileMapping失敗LastError=" + GetLastError().ToString() ); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? ? ? m_bInit = true; ?
??
? ? ? ? ? ? ? ? 映射第一塊文件 ?
? ? ? ? ? ? ? ? //m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ, 0, 0, (uint)m_MemSize); ?
? ? ? ? ? ? ? ? //if (m_pwData == IntPtr.Zero) ?
? ? ? ? ? ? ? ? //{ ?
? ? ? ? ? ? ? ? // ? ?m_bInit = false; ?
? ? ? ? ? ? ? ? // ? ?throw new Exception("m_hSharedMemoryFile失敗LastError=" + GetLastError().ToString()); ?
? ? ? ? ? ? ? ? //} ?
??
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取高32位 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="intValue"></param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private static uint GetHighWord( UInt64 intValue ) ?
? ? ? ? { ?
? ? ? ? ? ? return Convert.ToUInt32( intValue >> 32 ); ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取低32位 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="intValue"></param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private static uint GetLowWord( UInt64 intValue ) ?
? ? ? ? { ?
??
? ? ? ? ? ? return Convert.ToUInt32( intValue & 0x00000000FFFFFFFF ); ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取下一個文件塊 塊大小為20M ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns>false 表示已經是最后一塊文件</returns> ?
? ? ? ? public uint GetNextblock() ?
? ? ? ? { ?
? ? ? ? ? ? if ( !this.m_bInit ) throw new Exception( "文件未初始化。" ); ?
? ? ? ? ? ? //if ( m_offsetBegin + m_MemSize >= m_FileSize ) return false; ?
??
? ? ? ? ? ? uint m_Size = GetMemberSize(); ?
? ? ? ? ? ? if ( m_Size == 0 ) return m_Size; ?
??
? ? ? ? ? ? // 更改緩沖區大小 ?
? ? ? ? ? ? m_MemSize = m_Size; ?
??
? ? ? ? ? ? //卸載前一個文件 ?
? ? ? ? ? ? //bool l_result = UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? //m_pwData = IntPtr.Zero; ?
??
??
? ? ? ? ? ? m_pwData = MapViewOfFile( m_hSharedMemoryFile, FILE_MAP_READ, GetHighWord( ( UInt64 )m_offsetBegin ), GetLowWord( ( UInt64 )m_offsetBegin ), m_Size ); ?
? ? ? ? ? ? if ( m_pwData == IntPtr.Zero ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? m_bInit = false; ?
? ? ? ? ? ? ? ? throw new Exception( "映射文件塊失敗" + GetLastError().ToString() ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? m_offsetBegin = m_offsetBegin + m_Size; ?
??
? ? ? ? ? ? return m_Size; //創建成功 ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 返回映射區大小 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private uint GetMemberSize() ?
? ? ? ? { ?
? ? ? ? ? ? if ( m_offsetBegin >= m_FileSize ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return 0; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else if ( m_offsetBegin + m_MemSize >= m_FileSize ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? long temp = m_FileSize - m_offsetBegin; ?
? ? ? ? ? ? ? ? return ( uint )temp; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? return m_MemSize; ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 關閉內存映射 ?
? ? ? ? /// </summary> ?
? ? ? ? public void Close() ?
? ? ? ? { ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? ? ? CloseHandle( m_hSharedMemoryFile ); ?
? ? ? ? ? ? ? ? File.Close(); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當前塊中獲取數據 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數據</param> ?
? ? ? ? /// <param name="lngAddr">起始數據</param> ?
? ? ? ? /// <param name="lngSize">數據長度,最大值=緩沖長度</param> ?
? ? ? ? /// <param name="Unmap">讀取完成是否卸載緩沖區</param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public void Read( ref byte[] bytData, int lngAddr, int lngSize, bool Unmap ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "Read操作超出數據區" ); ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? // string bb = Marshal.PtrToStringAuto(m_pwData);// ?
? ? ? ? ? ? ? ? Marshal.Copy( m_pwData, bytData, lngAddr, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
??
? ? ? ? ? ? if ( Unmap ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? bool l_result = UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? ? ? if ( l_result ) ?
? ? ? ? ? ? ? ? ? ? m_pwData = IntPtr.Zero; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當前塊中獲取數據 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數據</param> ?
? ? ? ? /// <param name="lngAddr">起始數據</param> ?
? ? ? ? /// <param name="lngSize">數據長度,最大值=緩沖長度</param> ?
? ? ? ? /// <exception cref="Exception: Read操作超出數據區"></exception> ?
? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public void Read( ref byte[] bytData, int lngAddr, int lngSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "Read操作超出數據區" ); ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? Marshal.Copy( m_pwData, bytData, lngAddr, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當前塊中獲取數據 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="lngAddr">緩存區偏移量</param> ?
? ? ? ? /// <param name="byteData">數據數組</param> ?
? ? ? ? /// <param name="StartIndex">數據數組開始復制的下標</param> ?
? ? ? ? /// <param name="lngSize">數據長度,最大值=緩沖長度</param> ?
? ? ? ? /// <exception cref="Exception: 起始數據超過緩沖區長度"></exception> ?
? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception> ?
? ? ? ? /// <returns>返回實際讀取值</returns> ?
? ? ? ? public uint ReadBytes( int lngAddr, ref byte[] byteData, int StartIndex, uint intSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr >= m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "起始數據超過緩沖區長度" ); ?
??
? ? ? ? ? ? if ( lngAddr + intSize > m_MemSize ) ?
? ? ? ? ? ? ? ? intSize = m_MemSize - ( uint )lngAddr; ?
??
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? IntPtr s = new IntPtr( ( long )m_pwData + lngAddr ); // 地址偏移 ?
? ? ? ? ? ? ? ? Marshal.Copy( s, byteData, StartIndex, ( int )intSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
??
? ? ? ? ? ? return intSize; ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 寫數據 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數據</param> ?
? ? ? ? /// <param name="lngAddr">起始地址</param> ?
? ? ? ? /// <param name="lngSize">個數</param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private int Write( byte[] bytData, int lngAddr, int lngSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) return 2; //超出數據區 ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? Marshal.Copy( bytData, lngAddr, m_pwData, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return 1; //共享內存未初始化 ?
? ? ? ? ? ? } ?
? ? ? ? ? ? return 0; //寫成功 ?
? ? ? ? } ?
? ? } ?
? ? internal class FileReader ?
? ? { ?
? ? ? ? const uint GENERIC_READ = 0x80000000; ?
? ? ? ? const uint OPEN_EXISTING = 3; ?
? ? ? ? System.IntPtr handle; ?
??
? ? ? ? [DllImport( "kernel32", SetLastError = true )] ?
? ? ? ? public static extern System.IntPtr CreateFile( ?
? ? ? ? ? ? string FileName, ? ? ? ? ?// file name ?
? ? ? ? ? ? uint DesiredAccess, ? ? ? // access mode ?
? ? ? ? ? ? uint ShareMode, ? ? ? ? ? // share mode ?
? ? ? ? ? ? uint SecurityAttributes, ?// Security Attributes ?
? ? ? ? ? ? uint CreationDisposition, // how to create ?
? ? ? ? ? ? uint FlagsAndAttributes, ?// file attributes ?
? ? ? ? ? ? int hTemplateFile ? ? ? ? // handle to template file ?
? ? ? ? ); ?
??
? ? ? ? [System.Runtime.InteropServices.DllImport( "kernel32", SetLastError = true )] ?
? ? ? ? static extern bool CloseHandle ?
? ? ? ? ( ?
? ? ? ? ? ? System.IntPtr hObject // handle to object ?
? ? ? ? );?


? ? ? ? public IntPtr Open( string FileName ) ?
? ? ? ? { ?
? ? ? ? ? ? // open the existing file for reading ? ? ? ??
? ? ? ? ? ? handle = CreateFile ?
? ? ? ? ? ? ( ?
? ? ? ? ? ? ? ? FileName, ?
? ? ? ? ? ? ? ? GENERIC_READ, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? OPEN_EXISTING, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? 0 ?
? ? ? ? ? ? ); ?
??
? ? ? ? ? ? if ( handle != System.IntPtr.Zero ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return handle; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "打開文件失敗" ); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? public bool Close() ?
? ? ? ? { ?
? ? ? ? ? ? return CloseHandle( handle ); ?
? ? ? ? } ?
? ? } ?
} ?
========

.NET Framework自帶的文件內存映射類

http://www.cnblogs.com/briny/archive/2012/11/25/2787188.html
最近一直為文件內存映射發愁,整個兩周一直折騰這個東西。在64位系統和32位系統還要針對內存的高低位進行計算。好麻煩。。還是沒搞定
偶然從MSDN上發現.NET 4.0把內存文件映射加到了.NET類庫中。。好像方便了很多啊。。比用C#直接調用WINDOWS API方便多了。所以
這個必須果斷記錄之。。。項目馬上要用,為了加強內存數據交換的效率。。這個。。。必須啊。。


任務


使用的方法或屬性


從磁盤上的文件中獲取表示持久內存映射文件的 MemoryMappedFile 對象。
MemoryMappedFile.CreateFromFile 方法。
獲取表示非持久內存映射文件(與磁盤上的文件不關聯)的 MemoryMappedFile 對象。
MemoryMappedFile.CreateNew 方法。
- 或 -
MemoryMappedFile.CreateOrOpen 方法。
獲取現有內存映射文件(持久文件或非持久文件)的 MemoryMappedFile 對象。
MemoryMappedFile.OpenExisting 方法。
獲取針對內存映射文件的順序訪問視圖的 UnmanagedMemoryStream 對象。
MemoryMappedFile.CreateViewStream 方法。
獲取針對內存映射文件的隨機訪問視圖的 UnmanagedMemoryAccessor 對象。
MemoryMappedFile.CreateViewAccessor 方法。
獲取要用于非托管代碼的 SafeMemoryMappedViewHandle 對象。
MemoryMappedFile.SafeMemoryMappedFileHandle 屬性。
- 或 -
MemoryMappedViewAccessor.SafeMemoryMappedViewHandle 屬性。
- 或 -
MemoryMappedViewStream.SafeMemoryMappedViewHandle 屬性。
將內存分配推遲到創建視圖后進行(僅限于非持久文件)。
(若要確定當前系統頁大小,請使用 Environment.SystemPageSize 屬性。)
帶 MemoryMappedFileOptions.DelayAllocatePages 值的 CreateNew 方法。
- 或 -
將 MemoryMappedFileOptions 枚舉作為參數的 CreateOrOpen 方法。
?
持久文件內存映射:


CreateFromFile 方法基于磁盤上的現有文件創建一個內存映射文件。


?1 using System;
?2 using System.IO;
?3 using System.IO.MemoryMappedFiles;
?4 using System.Runtime.InteropServices;
?5?
?6 class Program
?7 {
?8 ? ? static void Main(string[] args)
?9 ? ? {
10 ? ? ? ? long offset = 0x10000000; // 256 megabytes
11 ? ? ? ? long length = 0x20000000; // 512 megabytes
12?
13 ? ? ? ? // Create the memory-mapped file.
14 ? ? ? ? using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
15 ? ? ? ? {
16 ? ? ? ? ? ? // Create a random access view, from the 256th megabyte (the offset)
17 ? ? ? ? ? ? // to the 768th megabyte (the offset plus length).
18 ? ? ? ? ? ? using (var accessor = mmf.CreateViewAccessor(offset, length))
19 ? ? ? ? ? ? {
20 ? ? ? ? ? ? ? ? int colorSize = Marshal.SizeOf(typeof(MyColor));
21 ? ? ? ? ? ? ? ? MyColor color;
22?
23 ? ? ? ? ? ? ? ? // Make changes to the view.
24 ? ? ? ? ? ? ? ? for (long i = 0; i < length; i += colorSize)
25 ? ? ? ? ? ? ? ? {
26 ? ? ? ? ? ? ? ? ? ? accessor.Read(i, out color);
27 ? ? ? ? ? ? ? ? ? ? color.Brighten(10);
28 ? ? ? ? ? ? ? ? ? ? accessor.Write(i, ref color);
29 ? ? ? ? ? ? ? ? }
30 ? ? ? ? ? ? }
31 ? ? ? ? }
32 ? ? }
33 }
34?
35 public struct MyColor
36 {
37 ? ? public short Red;
38 ? ? public short Green;
39 ? ? public short Blue;
40 ? ? public short Alpha;
41?
42 ? ? // Make the view brigher.
43 ? ? public void Brighten(short value)
44 ? ? {
45 ? ? ? ? Red = (short)Math.Min(short.MaxValue, (int)Red + value);
46 ? ? ? ? Green = (short)Math.Min(short.MaxValue, (int)Green + value);
47 ? ? ? ? Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
48 ? ? ? ? Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
49 ? ? }
50 }


?非持久文件內存映射:


CreateNew 和 CreateOrOpen 方法創建一個未映射到磁盤上的現有文件的內存映射文件。


?1 using System;
?2 using System.IO;
?3 using System.IO.MemoryMappedFiles;
?4 using System.Threading;
?5?
?6 class Program
?7 {
?8 ? ? // Process A:
?9 ? ? static void Main(string[] args)
10 ? ? {
11 ? ? ? ? using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
12 ? ? ? ? {
13 ? ? ? ? ? ? bool mutexCreated;
14 ? ? ? ? ? ? Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
15 ? ? ? ? ? ? using (MemoryMappedViewStream stream = mmf.CreateViewStream())
16 ? ? ? ? ? ? {
17 ? ? ? ? ? ? ? ? BinaryWriter writer = new BinaryWriter(stream);
18 ? ? ? ? ? ? ? ? writer.Write(1);
19 ? ? ? ? ? ? }
20 ? ? ? ? ? ? mutex.ReleaseMutex();
21?
22 ? ? ? ? ? ? Console.WriteLine("Start Process B and press ENTER to continue.");
23 ? ? ? ? ? ? Console.ReadLine();
24?
25 ? ? ? ? ? ? Console.WriteLine("Start Process C and press ENTER to continue.");
26 ? ? ? ? ? ? Console.ReadLine();
27?
28 ? ? ? ? ? ? mutex.WaitOne();
29 ? ? ? ? ? ? using (MemoryMappedViewStream stream = mmf.CreateViewStream())
30 ? ? ? ? ? ? {
31 ? ? ? ? ? ? ? ? BinaryReader reader = new BinaryReader(stream);
32 ? ? ? ? ? ? ? ? Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
33 ? ? ? ? ? ? ? ? Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
34 ? ? ? ? ? ? ? ? Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
35 ? ? ? ? ? ? }
36 ? ? ? ? ? ? mutex.ReleaseMutex();
37 ? ? ? ? }
38 ? ? }
39 }
========

總結

以上是生活随笔為你收集整理的C#内存映射文件学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。