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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

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)

適用於

另請參閱