using System; using System.IO; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; class Program { struct Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int X => x; public int Y => y; } static void Main() { int arrayLength = 10; int arraySize; unsafe { arraySize = sizeof(Point) * arrayLength; } using (var mmf = MemoryMappedFile.CreateFromFile("data.bin", FileMode.OpenOrCreate, "myData", arraySize)) { using (var accessor = mmf.CreateViewAccessor()) { unsafe { byte* ptr = null; try { accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); // 將內存映射的指針轉換爲 Point 數組 Point* pointArrayPtr = (Point*)ptr; // 在 Point 數組中進行讀寫操作 for (int i = 0; i < arrayLength; i++) { pointArrayPtr[i] = new Point(i,i); } // 從 Point 數組中讀取數據 for (int i = 0; i < arrayLength; i++) { Point value = pointArrayPtr[i]; Console.WriteLine($"x={value.X}, y={value.Y}"); } } finally { if (ptr != null) { accessor.SafeMemoryMappedViewHandle.ReleasePointer(); } } } } } } }
c#使用內存映射像處理內存一樣去快速處理文件
在 .NET Core 中,`System.IO.MemoryMappedFiles.MemoryMappedFile` 類提供了對內存映射文件的支持。通過將文件映射到內存,你可以在應用程序中直接訪問文件的內容,而不需要顯式地進行文件的讀取和寫入操作。
內存映射文件允許你將文件的特定區域映射到內存中的一個或多個 `MemoryMappedViewAccessor` 對象。`MemoryMappedViewAccessor` 提供了對映射區域的讀寫訪問。
同時,你可以使用 `MemoryMappedViewAccessor.SafeMemoryMappedViewHandle` 進行不安全操作,並將其映射到一個 數組,例如int[] 或者你自定義的Point[]。
通過這種方式,你可以直接訪問內存映射的數據,並對其進行更改,這將自動反映在映射文件中。
在下面的示例中,展示瞭如何使用 `MemoryMappedViewAccessor.SafeMemoryMappedViewHandle` 將內存映射文件映射到 `Point[]` 數組:
在這個示例中,首先創建一個內存映射文件,並指定了與 `Point[]` 數組相同大小的映射區域。然後,我們使用 `CreateViewAccessor` 方法創建一個 `MemoryMappedViewAccessor` 對象。
在 `unsafe` 上下文中,我們通過調用 `SafeMemoryMappedViewHandle.AcquirePointer` 方法來獲取內存映射的指針,然後將指針轉換爲 `Point*` 類型,從而將其視爲 `Point[]` 數組。
通過直接使用指針進行讀寫操作,你可以直接修改內存映射的數據,這將自動反映在映射文件中。最後,使用 `SafeMemoryMappedViewHandle.ReleasePointer` 方法釋放指針。
但是,使用不安全代碼需要謹慎,並且需要遵循安全性和正確性的最佳實踐。確保你正確管理內存和指針,並遵循 C# 中的不安全代碼規範。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.