Op Englesch liesen Editéieren

Deelen iwwer


MemoryMappedFile.CreateNew Method

Definition

Creates a memory-mapped file in system memory.

Overloads

CreateNew(String, Int64)

Creates a memory-mapped file that has the specified capacity 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, 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)

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

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

C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string? mapName, long capacity);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string mapName, long capacity);

Parameters

mapName
String

A name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64

The maximum size, in bytes, to allocate to the memory-mapped file.

Returns

A memory-mapped file that has the specified name and capacity.

Exceptions

mapName is an empty string.

capacity is less than or equal to zero.

.NET Core and .NET 5+ only: Calls to the CreateNew method with a named memory mapped file (that is, a non-null mapName) are supported on Windows operating systems only.

Examples

The following example is composed of three separate processes (console applications) that write Boolean values to a memory-mapped file. The following sequence of actions occur:

  1. Process A creates the memory-mapped file and writes a value to it.

  2. Process B opens the memory-mapped file and writes a value to it.

  3. Process C opens the memory-mapped file and writes a value to it.

  4. Process A reads and displays the values from the memory-mapped file.

  5. After Process A is finished with the memory-mapped file, the file is immediately reclaimed by garbage collection.

To run this example, do the following:

  1. Compile the applications and open three Command windows.

  2. In the first Command window, run Process A.

  3. In the second Command window, run Process B.

  4. Return to Process A and press ENTER.

  5. In the third Command window, run Process C.

  6. Return to Process A and press ENTER.

The output of Process A is as follows:

Start Process B and press ENTER to continue.
Start Process C and press ENTER to continue.
Process A says: True
Process B says: False
Process C says: True

Process A

C#
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;

class Program
{
    // Process A:
    static void Main(string[] args)
    {
        using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
        {
            bool mutexCreated;
            Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write(1);
            }
            mutex.ReleaseMutex();

            Console.WriteLine("Start Process B and press ENTER to continue.");
            Console.ReadLine();

            Console.WriteLine("Start Process C and press ENTER to continue.");
            Console.ReadLine();

            mutex.WaitOne();
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                BinaryReader reader = new BinaryReader(stream);
                Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
                Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
                Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
            }
            mutex.ReleaseMutex();
        }
    }
}

Process B

C#
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;

class Program
{
    // Process B:
    static void Main(string[] args)
    {
        try
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
            {

                Mutex mutex = Mutex.OpenExisting("testmapmutex");
                mutex.WaitOne();

                using (MemoryMappedViewStream stream = mmf.CreateViewStream(1, 0))
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write(0);
                }
                mutex.ReleaseMutex();
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Memory-mapped file does not exist. Run Process A first.");
        }
    }
}

Process C

C#
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;

class Program
{
    // Process C:
    static void Main(string[] args)
    {
        try
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
            {

                Mutex mutex = Mutex.OpenExisting("testmapmutex");
                mutex.WaitOne();

                using (MemoryMappedViewStream stream = mmf.CreateViewStream(2, 0))
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write(1);
                }
                mutex.ReleaseMutex();
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Memory-mapped file does not exist. Run Process A first, then B.");
        }
    }
}

Remarks

Use this method to create a memory-mapped file that is not persisted (that is, not associated with a file on disk), which you can use to share data between processes.

See also

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.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

CreateNew(String, Int64, MemoryMappedFileAccess)

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

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

C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string? mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);

Parameters

mapName
String

A name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64

The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess

One of the enumeration values that specifies the type of access allowed to the memory-mapped file. The default is ReadWrite.

Returns

A memory-mapped file that has the specified characteristics.

Exceptions

mapName is an empty string.

-or-

access is set to write-only with the Write enumeration value.

capacity is less than or equal to zero.

-or-

access is not a valid MemoryMappedFileAccess enumeration value.

.NET Core and .NET 5+ only: Calls to the CreateNew method with a named memory mapped file (that is, a non-null mapName) are supported on Windows operating systems only.

Remarks

Use this method to create a memory-mapped file that is not persisted (that is, not associated with a file on disk), which you can use to share data between processes.

See also

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.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

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

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

Creates a memory-mapped file that has the specified name, capacity, access type, memory allocation options and inheritability.

C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string? mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.MemoryMappedFiles.MemoryMappedFileOptions options, System.IO.HandleInheritability inheritability);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.MemoryMappedFiles.MemoryMappedFileOptions options, System.IO.HandleInheritability inheritability);

Parameters

mapName
String

A name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64

The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess

One of the enumeration values that specifies the type of access allowed to the memory-mapped file. The default is ReadWrite.

options
MemoryMappedFileOptions

A bitwise combination of enumeration values that specifies memory allocation options for the memory-mapped file.

inheritability
HandleInheritability

A value that specifies whether a handle to the memory-mapped file can be inherited by a child process. The default is None.

Returns

A memory-mapped file that has the specified characteristics.

Exceptions

mapName is an empty string.

-or-

access is set to write-only with the Write enumeration value.

capacity is less than or equal to zero.

-or-

access is not a valid MemoryMappedFileAccess enumeration value.

-or-

inheritability is not a valid HandleInheritability value.

.NET Core and .NET 5+ only: Calls to the CreateNew method with a named memory mapped file (that is, a non-null mapName) are supported on Windows operating systems only.

Applies to

.NET 10 an aner Versiounen
Produkt Versiounen
.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.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

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.

C#
[System.Security.SecurityCritical]
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.MemoryMappedFiles.MemoryMappedFileOptions options, System.IO.MemoryMappedFiles.MemoryMappedFileSecurity memoryMappedFileSecurity, System.IO.HandleInheritability inheritability);

Parameters

mapName
String

A name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64

The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess

One of the enumeration values that specifies the type of access allowed to the memory-mapped file. The default is ReadWrite.

options
MemoryMappedFileOptions

A bitwise combination of enumeration values that specifies memory allocation options for the memory-mapped file.

memoryMappedFileSecurity
MemoryMappedFileSecurity

The permissions that can be granted for file access and operations on memory-mapped files.

This parameter can be null.

inheritability
HandleInheritability

One of the enumeration values that specifies whether a handle to the memory-mapped file can be inherited by a child process. The default is None.

Returns

A memory-mapped file that has the specified characteristics.

Attributes

Exceptions

mapName is an empty string.

-or-

access is set to write-only with the Write enumeration value.

capacity is less than or equal to zero.

-or-

access is not a valid MemoryMappedFileAccess enumeration value.

-or-

inheritability is not a valid HandleInheritability enumeration value.

Remarks

Use this method to create a memory-mapped file that is not persisted (that is, not associated with a file on disk), which you can use to share data between processes.

See also

Applies to

.NET Framework 4.8.1 an aner Versiounen
Produkt Versiounen
.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