This documentation is archived and is not being maintained.

MemoryMappedFile.CreateFromFile Method (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.

Namespace:  System.IO.MemoryMappedFiles
Assembly:  System.Core (in System.Core.dll)

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static MemoryMappedFile CreateFromFile(
	string path,
	FileMode mode,
	string mapName,
	long capacity,
	MemoryMappedFileAccess access
)

Parameters

path
Type: System.String
The path to the file to map.
mode
Type: System.IO.FileMode
Access mode; can be any of the FileMode enumeration values except Append.
mapName
Type: System.String
A name to assign to the memory-mapped file.
capacity
Type: System.Int64
The maximum size, in bytes, to allocate to the memory-mapped file. Specify 0 to set the capacity to the size of the file on disk.
access
Type: System.IO.MemoryMappedFiles.MemoryMappedFileAccess
One of the enumeration values that specifies the type of access allowed to the memory-mapped file.

Return Value

Type: System.IO.MemoryMappedFiles.MemoryMappedFile
A memory-mapped file that has the specified characteristics.

ExceptionCondition
ArgumentException

mapName is an empty string.

-or-

access is not an allowed value.

-or-

path specifies an empty file.

-or-

access is specified as Read and capacity is greater than the size of the file indicated by path.

-or-

mode is Append.

ArgumentNullException

path or mapName is null.

ArgumentOutOfRangeException

capacity is greater than the size of the logical address space.

-or-

capacity is less than zero.

-or-

capacity is less than the file size (but not zero).

-or-

capacity is zero, and the size of the file on disk is also zero.

-or-

access is not a defined MemoryMappedFileAccess value.

-or-

The size of the file indicated by path is greater than capacity.

IOException

-or-

An I/O error occurred.

PathTooLongException

path exceeds the maximum length defined by the operating system. In Windows, paths must contain fewer than 248 characters, and file names must contain fewer than 260 characters.

SecurityException

The caller does not have the required permissions for the file.

The mode parameter pertains to the source file on disk.

If capacity is larger than the size of the file on disk, the file on disk is increased to match the specified capacity even if no data is written to the memory-mapped file. To prevent this from occurring, specify 0 (zero) for the default capacity, which will internally set capacity to the size of the file on disk.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: