MemoryMappedFile 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示内存映射文件。
public ref class MemoryMappedFile : IDisposablepublic class MemoryMappedFile : IDisposabletype MemoryMappedFile = class
    interface IDisposablePublic Class MemoryMappedFile
Implements IDisposable- 继承
- 
				MemoryMappedFile
- 实现
示例
下面的示例为极大文件的一部分创建内存映射视图,并控制其中一部分。
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
class Program
{
    static void Main(string[] args)
    {
        long offset = 0x10000000; // 256 megabytes
        long length = 0x20000000; // 512 megabytes
        // Create the memory-mapped file.
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
        {
            // Create a random access view, from the 256th megabyte (the offset)
            // to the 768th megabyte (the offset plus length).
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;
                // Make changes to the view.
                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(10);
                    accessor.Write(i, ref color);
                }
            }
        }
    }
}
public struct MyColor
{
    public short Red;
    public short Green;
    public short Blue;
    public short Alpha;
    // Make the view brighter.
    public void Brighten(short value)
    {
        Red = (short)Math.Min(short.MaxValue, (int)Red + value);
        Green = (short)Math.Min(short.MaxValue, (int)Green + value);
        Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
        Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    }
}
Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Runtime.InteropServices
Class Program
    Sub Main()
        Dim offset As Long = &H10000000 ' 256 megabytes
        Dim length As Long = &H20000000 ' 512 megabytes
        ' Create the memory-mapped file.
        Using mmf = MemoryMappedFile.CreateFromFile("c:\ExtremelyLargeImage.data", FileMode.Open, "ImgA")
            ' Create a random access view, from the 256th megabyte (the offset)
            ' to the 768th megabyte (the offset plus length).
            Using accessor = mmf.CreateViewAccessor(offset, length)
                Dim colorSize As Integer = Marshal.SizeOf(GetType(MyColor))
                Dim color As MyColor
                Dim i As Long = 0
                ' Make changes to the view.
                Do While (i < length)
                    accessor.Read(i, color)
                    color.Brighten(10)
                    accessor.Write(i, color)
                    i += colorSize
                Loop
            End Using
        End Using
    End Sub
End Class
Public Structure MyColor
    Public Red As Short
    Public Green As Short
    Public Blue As Short
    Public Alpha As Short
    ' Make the view brighter.
    Public Sub Brighten(ByVal value As Short)
        Red = CType(Math.Min(Short.MaxValue, (CType(Red, Integer) + value)), Short)
        Green = CType(Math.Min(Short.MaxValue, (CType(Green, Integer) + value)), Short)
        Blue = CType(Math.Min(Short.MaxValue, (CType(Blue, Integer) + value)), Short)
        Alpha = CType(Math.Min(Short.MaxValue, (CType(Alpha, Integer) + value)), Short)
    End Sub
End Structure
注解
内存映射文件将文件的内容映射到应用程序的逻辑地址空间。 内存映射文件使程序员能够处理非常大的文件,因为内存可以同时管理,并且允许完全随机访问文件,而无需查找。 内存映射文件也可以跨多个进程共享。
方法 CreateFromFile 从指定路径或 FileStream 磁盘上现有文件创建内存映射文件。 取消映射文件时,更改会自动传播到磁盘。
方法 CreateNew 创建一个内存映射文件,该文件未映射到磁盘上的现有文件;并且适用于为 IPC) (进程间通信创建共享内存。
内存映射文件可以与允许与其他进程共享内存映射文件的可选名称相关联。
可以创建内存映射文件的多个视图,包括文件部分的视图。 可以将文件的同一部分映射到多个地址,以创建并发内存。 若要让两个视图一直处于并发状态,必须通过同一个内存映射文件创建它们。 使用两个视图创建同一文件的两个文件映射不提供并发性。
属性
| SafeMemoryMappedFileHandle | 获取内存映射文件的文件句柄。 |