MemoryMappedFile 类

定义

表示内存映射文件。

public ref class MemoryMappedFile : IDisposable
public class MemoryMappedFile : IDisposable
type MemoryMappedFile = class
    interface IDisposable
Public 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

获取内存映射文件的文件句柄。

方法

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)

从现有文件创建一个具有指定的访问模式、名称、继承性和容量的内存映射文件。

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)

基于磁盘上的文件创建一个具有指定名称、容量、访问类型、安全权限、继承性和释放要求的内存映射文件。

CreateFromFile(SafeFileHandle, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)

使用 SafeFileHandle 和指定的访问模式、名称、可继承性和容量从现有文件创建内存映射文件。

CreateFromFile(String)

基于磁盘上的文件创建一个内存映射文件。

CreateFromFile(String, FileMode)

基于磁盘上的文件创建一个具有指定访问模式的内存映射文件。

CreateFromFile(String, FileMode, String)

基于磁盘上的文件创建一个具有指定访问模式和名称的内存映射文件。

CreateFromFile(String, FileMode, String, Int64)

基于磁盘上的文件创建一个具有指定访问模式、名称和容量的内存映射文件。

CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)

基于磁盘上的文件创建一个具有指定访问模式、名称、容量和访问类型的内存映射文件。

CreateNew(String, Int64)

在系统内存中创建一个具有指定容量的内存映射文件。

CreateNew(String, Int64, MemoryMappedFileAccess)

在系统内存中创建一个具有指定容量和访问类型的内存映射文件。

CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)

创建一个具有指定名称、容量、访问类型、内存分配选项和继承性的内存映射文件。

CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)

在系统内存中创建一个具有指定容量、访问类型、内存分配、安全权限和继承性的内存映射文件。

CreateOrOpen(String, Int64)

在系统内存中创建或打开具有指定名称和容量的内存映射文件。

CreateOrOpen(String, Int64, MemoryMappedFileAccess)

在系统内存中创建或打开具有指定名称、容量和访问类型的内存映射文件。

CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)

创建一个新的空内存映射文件或打开一个现有的内存映射文件(如果存在同名的内存映射文件)。 如果打开现有的文件,则会忽略容量、选项和内存参数。

CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)

在系统内存中创建或打开具有指定名称、容量、访问类型、内存分配、安全权限和继承性的内存映射文件。

CreateViewAccessor()

创建映射到内存映射文件视图的 MemoryMappedViewAccessor

CreateViewAccessor(Int64, Int64)

创建一个 MemoryMappedViewAccessor,它映射到内存映射文件的视图并具有指定的偏移和大小。

CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)

创建一个 MemoryMappedViewAccessor,它映射到内存映射文件的视图并具有指定的偏移、大小和访问限制。

CreateViewStream()

创建映射到内存映射文件视图的流。

CreateViewStream(Int64, Int64)

创建一个流,它映射到内存映射文件的视图并具有指定的偏移和大小。

CreateViewStream(Int64, Int64, MemoryMappedFileAccess)

创建一个流,它映射到内存映射文件的视图,并具有指定的偏移、大小和访问类型。

Dispose()

释放由 MemoryMappedFile 使用的所有资源。

Dispose(Boolean)

释放由 MemoryMappedFile 占用的非托管资源,还可以另外再释放托管资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetAccessControl()

获取对内存映射文件资源的访问控制。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OpenExisting(String)

在系统内存中打开一个具有指定名称的现有内存映射文件。

OpenExisting(String, MemoryMappedFileRights)

在系统内存中打开一个具有指定名称和访问权限的现有内存映射文件。

OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

在系统内存中打开一个具有指定名称、访问权限和继承性的现有内存映射文件。

SetAccessControl(MemoryMappedFileSecurity)

设置对内存映射文件资源的访问控制。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅