Читати англійською Редагувати

Поділитися через


MemoryMappedFile Class

Definition

Represents a memory-mapped file.

C#
public class MemoryMappedFile : IDisposable
Inheritance
MemoryMappedFile
Implements

Examples

The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.

C#
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);
    }
}

Remarks

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.

The 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.

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. Creating two file mappings of the same file with two views does not provide concurrency.

Properties

SafeMemoryMappedFileHandle

Gets the file handle of a memory-mapped file.

Methods

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)

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(SafeFileHandle, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)

Creates a memory-mapped file from an existing file using a SafeFileHandle and the specified access mode, name, inheritability, and capacity.

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.

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)

Creates a memory-mapped file that has the specified access mode and name from a file on disk.

CreateFromFile(String, FileMode)

Creates a memory-mapped file that has the specified access mode from a file on disk.

CreateFromFile(String)

Creates a memory-mapped file from a file on disk.

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)

Creates a memory-mapped file that has the specified capacity, access type, memory allocation, security permissions, and inheritability in system memory.

CreateNew(String, Int64, MemoryMappedFileAccess)

Creates a memory-mapped file that has the specified capacity and access type in system memory.

CreateNew(String, Int64)

Creates a memory-mapped file that has the specified capacity in system memory.

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)

Creates or opens a memory-mapped file that has the specified name, capacity, access type, memory allocation, security permissions, and inheritability in system memory.

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)

Creates or opens a memory-mapped file that has the specified name and capacity in system memory.

CreateViewAccessor()

Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file.

CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)

Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset, size, and access restrictions.

CreateViewAccessor(Int64, Int64)

Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset and size.

CreateViewStream()

Creates a stream that maps to a view of the memory-mapped file.

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.

CreateViewStream(Int64, Int64)

Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset and size.

Dispose()

Releases all resources used by the MemoryMappedFile.

Dispose(Boolean)

Releases the unmanaged resources used by the MemoryMappedFile and optionally releases the managed resources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAccessControl()

Gets the access control to the memory-mapped file resource.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

Opens an existing memory-mapped file that has the specified name, access rights, and inheritability in system memory.

OpenExisting(String, MemoryMappedFileRights)

Opens an existing memory-mapped file that has the specified name and access rights in system memory.

OpenExisting(String)

Opens an existing memory-mapped file that has the specified name in system memory.

SetAccessControl(MemoryMappedFileSecurity)

Sets the access control to the memory-mapped file resource.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also