MemoryMappedFile.CreateNew MemoryMappedFile.CreateNew MemoryMappedFile.CreateNew MemoryMappedFile.CreateNew Method

定義

在系統記憶體中建立記憶體對應檔。Creates a memory-mapped file in system memory.

多載

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)

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

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.

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateNew(System::String ^ mapName, long capacity);
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew (string mapName, long capacity);
static member CreateNew : string * int64 -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateNew (mapName As String, capacity As Long) As MemoryMappedFile

參數

mapName
String String String String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 nullA name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64 Int64 Int64 Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。The maximum size, in bytes, to allocate to the memory-mapped file.

傳回

記憶體對應檔,具有指定的名稱與大小。A memory-mapped file that has the specified name and capacity.

例外狀況

mapName 為空字串。mapName is an empty string.

capacity 小於或等於零。capacity is less than or equal to zero.

僅限 .NET Core:只有在 Windows 作業系統上,才支援使用具名記憶體對應檔案 (亦即非 Null 的 mapName) 呼叫 CreateNew 方法。.NET Core 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.

範例

下列範例是由三個不同的進程 (主控台應用程式) 所Boolean組成, 可將值寫入記憶體對應檔案。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. 進程 A 會建立記憶體對應檔, 並將值寫入其中。Process A creates the memory-mapped file and writes a value to it.

  2. 進程 B 會開啟記憶體對應檔案, 並在其中寫入值。Process B opens the memory-mapped file and writes a value to it.

  3. 進程 C 會開啟記憶體對應檔案, 並在其中寫入值。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. 在進程 A 完成記憶體對應檔案後, 垃圾收集會立即回收檔案。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. 在第一個命令視窗中, 執行 [處理 A]。In the first Command window, run Process A.

  3. 在第二個命令視窗中, 執行進程 B。In the second Command window, run Process B.

  4. 返回 [處理 A], 然後按 ENTER 鍵。Return to Process A and press ENTER.

  5. 在第三個命令視窗中, 執行進程 C。In the third Command window, run Process C.

  6. 返回 [處理 A], 然後按 ENTER 鍵。Return to Process A and press ENTER.

進程 A 的輸出如下所示: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 AProcess 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();
        }
    }
}
Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Threading

Module Module1

    ' Process A:
    Sub Main()
        Using mmf As MemoryMappedFile = MemoryMappedFile.CreateNew("testmap", 10000)
            Dim mutexCreated As Boolean
            Dim mTex As Mutex = New Mutex(True, "testmapmutex", mutexCreated)
            Using Stream As MemoryMappedViewStream = mmf.CreateViewStream()
                Dim writer As BinaryWriter = New BinaryWriter(Stream)
                writer.Write(1)
            End Using
            mTex.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()

            mTex.WaitOne()
            Using Stream As MemoryMappedViewStream = mmf.CreateViewStream()
                Dim reader As BinaryReader = 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())
            End Using
            mTex.ReleaseMutex()

        End Using

    End Sub

End Module

Process BProcess 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.");
        }
    }
}
Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Threading

Module Module1
    ' Process B:
    Sub Main()
        Try
            Using mmf As MemoryMappedFile = MemoryMappedFile.OpenExisting("testmap")
                Dim mTex As Mutex = Mutex.OpenExisting("testmapmutex")
                mTex.WaitOne()
                Using Stream As MemoryMappedViewStream = mmf.CreateViewStream(1, 0)
                    Dim writer As BinaryWriter = New BinaryWriter(Stream)
                    writer.Write(0)
                End Using
                mTex.ReleaseMutex()
            End Using
        Catch noFile As FileNotFoundException
            Console.WriteLine("Memory-mapped file does not exist. Run Process A first." & vbCrLf & noFile.Message)
        End Try

    End Sub

End Module

Process CProcess 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.");
        }
    }
}
Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Threading

Module Module1
    ' Process C:
    Sub Main()
        Try
            Using mmf As MemoryMappedFile = MemoryMappedFile.OpenExisting("testmap")
                Dim mTex As Mutex = Mutex.OpenExisting("testmapmutex")
                mTex.WaitOne()
                Using Stream As MemoryMappedViewStream = mmf.CreateViewStream(2, 0)
                    Dim writer As BinaryWriter = New BinaryWriter(Stream)
                    writer.Write(1)
                End Using
                mTex.ReleaseMutex()
            End Using
        Catch noFile As FileNotFoundException
            Console.WriteLine("Memory-mapped file does not exist. Run Process A first, then B." & vbCrLf & noFile.Message)
        End Try

    End Sub

End Module

備註

使用此方法來建立不會保存的記憶體對應檔案 (也就是與磁片上的檔案沒有關聯), 您可以使用此檔案在進程之間共用資料。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.

另請參閱

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.

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateNew(System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access);
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew (string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);
static member CreateNew : string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateNew (mapName As String, capacity As Long, access As MemoryMappedFileAccess) As MemoryMappedFile

參數

mapName
String String String String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 nullA name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64 Int64 Int64 Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。One of the enumeration values that specifies the type of access allowed to the memory-mapped file. 預設為 ReadWriteThe default is ReadWrite.

傳回

記憶體對應檔,具有指定的特性。A memory-mapped file that has the specified characteristics.

例外狀況

mapName 為空字串。mapName is an empty string.

-或--or- access 設為唯寫,且具有 Write 列舉值。access is set to write-only with the Write enumeration value.

capacity 小於或等於零。capacity is less than or equal to zero.

-或--or- access 不是有效的 MemoryMappedFileAccess 列舉值。access is not a valid MemoryMappedFileAccess enumeration value.

僅限 .NET Core:只有在 Windows 作業系統上,才支援使用具名記憶體對應檔案 (亦即非 Null 的 mapName) 呼叫 CreateNew 方法。.NET Core 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.

備註

使用此方法來建立不會保存的記憶體對應檔案 (也就是與磁片上的檔案沒有關聯), 您可以使用此檔案在進程之間共用資料。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.

另請參閱

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.

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateNew(System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access, System::IO::MemoryMappedFiles::MemoryMappedFileOptions options, System::IO::HandleInheritability inheritability);
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);
static member CreateNew : string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.MemoryMappedFiles.MemoryMappedFileOptions * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateNew (mapName As String, capacity As Long, access As MemoryMappedFileAccess, options As MemoryMappedFileOptions, inheritability As HandleInheritability) As MemoryMappedFile

參數

mapName
String String String String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 nullA name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64 Int64 Int64 Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。One of the enumeration values that specifies the type of access allowed to the memory-mapped file. 預設為 ReadWriteThe default is ReadWrite.

options
MemoryMappedFileOptions MemoryMappedFileOptions MemoryMappedFileOptions MemoryMappedFileOptions

列舉值的位元組合,指定記憶體對應檔的記憶體配置選項。A bitwise combination of enumeration values that specifies memory allocation options for the memory-mapped file.

inheritability
HandleInheritability HandleInheritability HandleInheritability HandleInheritability

決定記憶體對應檔的控制代碼是否可以由子處理序繼承的值。A value that specifies whether a handle to the memory-mapped file can be inherited by a child process. 預設為 NoneThe default is None.

傳回

記憶體對應檔,具有指定的特性。A memory-mapped file that has the specified characteristics.

例外狀況

mapName 為空字串。mapName is an empty string.

-或--or- access 設為唯寫,且具有 Write 列舉值。access is set to write-only with the Write enumeration value.

capacity 小於或等於零。capacity is less than or equal to zero.

-或--or- access 不是有效的 MemoryMappedFileAccess 列舉值。access is not a valid MemoryMappedFileAccess enumeration value.

-或--or- inheritability 不是有效的 HandleInheritability 值。inheritability is not a valid HandleInheritability value.

僅限 .NET Core:只有在 Windows 作業系統上,才支援使用具名記憶體對應檔案 (亦即非 Null 的 mapName) 呼叫 CreateNew 方法。.NET Core 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.

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.

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateNew(System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access, System::IO::MemoryMappedFiles::MemoryMappedFileOptions options, System::IO::MemoryMappedFiles::MemoryMappedFileSecurity ^ memoryMappedFileSecurity, System::IO::HandleInheritability inheritability);
[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);
static member CreateNew : string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.MemoryMappedFiles.MemoryMappedFileOptions * System.IO.MemoryMappedFiles.MemoryMappedFileSecurity * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile

參數

mapName
String String String String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 nullA name to assign to the memory-mapped file, or null for a MemoryMappedFile that you do not intend to share across processes.

capacity
Int64 Int64 Int64 Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。The maximum size, in bytes, to allocate to the memory-mapped file.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。One of the enumeration values that specifies the type of access allowed to the memory-mapped file. 預設為 ReadWriteThe default is ReadWrite.

options
MemoryMappedFileOptions MemoryMappedFileOptions MemoryMappedFileOptions MemoryMappedFileOptions

列舉值的位元組合,指定記憶體對應檔的記憶體配置選項。A bitwise combination of enumeration values that specifies memory allocation options for the memory-mapped file.

memoryMappedFileSecurity
MemoryMappedFileSecurity MemoryMappedFileSecurity MemoryMappedFileSecurity MemoryMappedFileSecurity

允許對記憶體對應檔進行檔案存取及作業的可授與權限。The permissions that can be granted for file access and operations on memory-mapped files.

這個參數可以是 nullThis parameter can be null.

inheritability
HandleInheritability HandleInheritability HandleInheritability HandleInheritability

其中一個列舉值,決定記憶體對應檔的控制代碼是否可以由子處理序繼承。One of the enumeration values that specifies whether a handle to the memory-mapped file can be inherited by a child process. 預設為 NoneThe default is None.

傳回

記憶體對應檔,具有指定的特性。A memory-mapped file that has the specified characteristics.

例外狀況

mapName 為空字串。mapName is an empty string.

-或--or- access 設為唯寫,且具有 Write 列舉值。access is set to write-only with the Write enumeration value.

capacity 小於或等於零。capacity is less than or equal to zero.

-或--or- access 不是有效的 MemoryMappedFileAccess 列舉值。access is not a valid MemoryMappedFileAccess enumeration value.

-或--or- inheritability 不是有效的 HandleInheritability 列舉值。inheritability is not a valid HandleInheritability enumeration value.

備註

使用此方法來建立不會保存的記憶體對應檔案 (也就是與磁片上的檔案沒有關聯), 您可以使用此檔案在進程之間共用資料。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.

安全性

SecurityPermission
用於存取未受管理的程式碼。for access to unmanaged code. 關聯的列舉型別:UnmanagedCodeAssociated enumeration: UnmanagedCode.

另請參閱

適用於