MemoryMappedFile.CreateNew Method (String, Int64)
Creates a memory-mapped file that has the specified capacity in system memory.
Namespace: System.IO.MemoryMappedFiles
Assembly: System.Core (in System.Core.dll)
Parameters
- 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.
Return Value
Type: System.IO.MemoryMappedFiles.MemoryMappedFileA memory-mapped file that has the specified name and capacity.
| Exception | Condition |
|---|---|
| ArgumentException | mapName is an empty string. |
| ArgumentNullException | mapName is null. |
| ArgumentOutOfRangeException | capacity is less than or equal to zero. |
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:
Process A creates the memory-mapped file and writes a value to it.
Process B opens the memory-mapped file and writes a value to it.
Process C opens the memory-mapped file and writes a value to it.
Process A reads and displays the values from the memory-mapped file.
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:
Compile the applications and open three Command windows.
In the first Command window, run Process A.
In the second Command window, run Process B.
Return to Process A and press ENTER.
In the third Command window, run Process C.
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
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
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
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."); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.