MemoryMappedFile MemoryMappedFile MemoryMappedFile MemoryMappedFile Class

定義

表示記憶體對應檔。Represents a memory-mapped file.

public ref class MemoryMappedFile : IDisposable
public class MemoryMappedFile : IDisposable
type MemoryMappedFile = class
    interface IDisposable
Public Class MemoryMappedFile
Implements IDisposable
繼承
MemoryMappedFileMemoryMappedFileMemoryMappedFileMemoryMappedFile
實作

範例

下列範例會針對極大檔案的一部分建立記憶體對應檢視,及操作其中的一部分。The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.

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

備註

記憶體對應檔案會將檔案的內容對應至應用程式的邏輯位址空間。A memory-mapped file maps the contents of a file to an application's logical address space. 記憶體對應檔案可讓程式設計人員處理極大的檔案, 因為記憶體可以同時進行管理, 且允許完整的隨機存取檔案, 而不需要進行搜尋。Memory-mapped files enable programmers to work with extremely large files because memory can be managed concurrently, and they allow complete, random access to a file without the need for seeking. 記憶體對應檔案也可以跨多個進程共用。Memory-mapped files can also be shared across multiple processes.

方法會從指定的路徑FileStream或磁片上現有檔案的建立記憶體對應檔案。 CreateFromFileThe CreateFromFile methods create a memory-mapped file from a specified path or a FileStream of an existing file on disk. 當檔案未對應時, 變更會自動傳播至磁片。Changes are automatically propagated to disk when the file is unmapped.

CreateNew方法會建立記憶體對應檔案, 而該檔案未對應到磁片上的現有檔案; 而且適用于建立處理序間通訊 (IPC) 的共用記憶體。The CreateNew methods create a memory-mapped file that is not mapped to an existing file on disk; and are suitable for creating shared memory for interprocess communication (IPC).

記憶體對應檔可以與選擇性名稱相關聯, 讓記憶體對應檔案與其他進程共用。A memory-mapped file can be associated with an optional name that allows the memory-mapped file to be shared with other processes.

您可以建立多個記憶體對應檔案的視圖, 包括檔案部分的查看。You can create multiple views of the memory-mapped file, including views of parts of the file. 您可以將檔案的相同部分對應到一個以上的位址, 以建立並行記憶體。You can map the same part of a file to more than one address to create concurrent memory. 若要讓兩個檢視維持並行,必須從相同的記憶體對應檔案建立這兩個檢視。For two views to remain concurrent, they have to be created from the same memory-mapped file. 以兩個 views 建立相同檔案的兩個檔案對應並不會提供平行存取。Creating two file mappings of the same file with two views does not provide concurrency.

屬性

SafeMemoryMappedFileHandle SafeMemoryMappedFileHandle SafeMemoryMappedFileHandle SafeMemoryMappedFileHandle

取得記憶體對應檔的檔案控制代碼。Gets the file handle of a memory-mapped file.

方法

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

從現有的檔案建立記憶體對應檔,這個檔案具有指定的存取模式、名稱、可繼承性和容量。Creates a memory-mapped file from an existing file with the specified access mode, name, inheritability, and capacity.

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

從磁碟上的檔案建立記憶體對應檔案,這個記憶體對應檔具有指定的名稱、大小、存取類型、安全性權限、可繼承性和處置需求。Creates a memory-mapped file that has the specified name, capacity, access type, security permissions, inheritability, and disposal requirement from a file on disk.

CreateFromFile(String) CreateFromFile(String) CreateFromFile(String) CreateFromFile(String)

從磁碟上的檔案建立記憶體對應檔。Creates a memory-mapped file from a file on disk.

CreateFromFile(String, FileMode) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式。Creates a memory-mapped file that has the specified access mode from a file on disk.

CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式和名稱。Creates a memory-mapped file that has the specified access mode and name from a file on disk.

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

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱和大小。Creates a memory-mapped file that has the specified access mode, name, and capacity from a file on disk.

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

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱、大小和存取類型。Creates a memory-mapped file that has the specified access mode, name, capacity, and access type from a file on disk.

CreateNew(String, Int64) CreateNew(String, Int64) CreateNew(String, Int64) CreateNew(String, Int64)

建立記憶體對應檔案,此檔案在系統記憶體中具有指定的大小。Creates a memory-mapped file that has the specified capacity in system memory.

CreateNew(String, Int64, MemoryMappedFileAccess) CreateNew(String, Int64, MemoryMappedFileAccess) CreateNew(String, Int64, MemoryMappedFileAccess) CreateNew(String, Int64, MemoryMappedFileAccess)

建立記憶體對應檔案,此檔案在系統記憶體中具有指定的大小和存取類型。Creates a memory-mapped file that has the specified capacity and access type in system memory.

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

建立記憶體對應檔,這個檔案具有指定的名稱、容量、存取類型、記憶體配置選項和可繼承性。Creates a memory-mapped file that has the specified name, capacity, access type, memory allocation options and inheritability.

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

建立記憶體對應檔案,此檔案在系統記憶體中具有指定的大小、存取類型、記憶體配置、安全性權限以及可繼承性。Creates a memory-mapped file that has the specified capacity, access type, memory allocation, security permissions, and inheritability in system memory.

CreateOrOpen(String, Int64) CreateOrOpen(String, Int64) CreateOrOpen(String, Int64) CreateOrOpen(String, Int64)

建立或開啟記憶體對應檔,此檔案在系統記憶體中具有指定的名稱和容量。Creates or opens a memory-mapped file that has the specified name and capacity in system memory.

CreateOrOpen(String, Int64, MemoryMappedFileAccess) CreateOrOpen(String, Int64, MemoryMappedFileAccess) CreateOrOpen(String, Int64, MemoryMappedFileAccess) CreateOrOpen(String, Int64, MemoryMappedFileAccess)

建立或開啟記憶體對應檔,此檔案在系統記憶體中具有指定的名稱、容量和存取類型。Creates or opens a memory-mapped file that has the specified name, capacity and access type in system memory.

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

建立新的空白記憶體對應檔,或開啟現有的記憶體對應檔 (如果有名稱相同的檔案存在)。Creates a new empty memory mapped file or opens an existing memory mapped file if one exists with the same name. 如果開啟現有的檔案,則會忽略容量、選項和記憶體引數。If opening an existing file, the capacity, options, and memory arguments will be ignored.

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

建立或開啟記憶體對應檔,此檔案在系統記憶體中具有指定的名稱、容量、存取類型、記憶體配置、安全性權限以及可繼承性。Creates or opens a memory-mapped file that has the specified name, capacity, access type, memory allocation, security permissions, and inheritability in system memory.

CreateViewAccessor() CreateViewAccessor() CreateViewAccessor() CreateViewAccessor()

建立對應於記憶體對應檔檢視的 MemoryMappedViewAccessorCreates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file.

CreateViewAccessor(Int64, Int64) CreateViewAccessor(Int64, Int64) CreateViewAccessor(Int64, Int64) CreateViewAccessor(Int64, Int64)

建立 MemoryMappedViewAccessor,此存取子可對應到記憶體對應檔案的檢視,並且有指定的位移和大小。Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset and size.

CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess) CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess) CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess) CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)

建立 MemoryMappedViewAccessor,此存取子可對應到記憶體對應檔案的檢視,並且有指定位移、大小和存取限制。Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset, size, and access restrictions.

CreateViewStream() CreateViewStream() CreateViewStream() CreateViewStream()

建立對應於記憶體對應檔檢視的資料流。Creates a stream that maps to a view of the memory-mapped file.

CreateViewStream(Int64, Int64) CreateViewStream(Int64, Int64) CreateViewStream(Int64, Int64) CreateViewStream(Int64, Int64)

建立資料流,此資料流可對應到記憶體對應檔案的檢視,並且有指定的位移和大小。Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset and size.

CreateViewStream(Int64, Int64, MemoryMappedFileAccess) CreateViewStream(Int64, Int64, MemoryMappedFileAccess) CreateViewStream(Int64, Int64, MemoryMappedFileAccess) CreateViewStream(Int64, Int64, MemoryMappedFileAccess)

建立資料流,此資料流可對應到記憶體對應檔案的檢視,並且有指定位移、大小和存取類型。Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset, size, and access type.

Dispose() Dispose() Dispose() Dispose()

釋放 MemoryMappedFile 所使用的所有資源。Releases all resources used by the MemoryMappedFile.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 MemoryMappedFile 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the MemoryMappedFile and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAccessControl() GetAccessControl() GetAccessControl() GetAccessControl()

取得記憶體對應檔資源的存取控制。Gets the access control to the memory-mapped file resource.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OpenExisting(String) OpenExisting(String) OpenExisting(String) OpenExisting(String)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱。Opens an existing memory-mapped file that has the specified name in system memory.

OpenExisting(String, MemoryMappedFileRights) OpenExisting(String, MemoryMappedFileRights) OpenExisting(String, MemoryMappedFileRights) OpenExisting(String, MemoryMappedFileRights)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱和存取權限。Opens an existing memory-mapped file that has the specified name and access rights in system memory.

OpenExisting(String, MemoryMappedFileRights, HandleInheritability) OpenExisting(String, MemoryMappedFileRights, HandleInheritability) OpenExisting(String, MemoryMappedFileRights, HandleInheritability) OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱、存取權限和可繼承性。Opens an existing memory-mapped file that has the specified name, access rights, and inheritability in system memory.

SetAccessControl(MemoryMappedFileSecurity) SetAccessControl(MemoryMappedFileSecurity) SetAccessControl(MemoryMappedFileSecurity) SetAccessControl(MemoryMappedFileSecurity)

設定記憶體對應檔資源的存取控制。Sets the access control to the memory-mapped file resource.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱