MemoryMappedFile.CreateNew 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在系统内存中创建内存映射的文件。
重载
| 名称 | 说明 |
|---|---|
| CreateNew(String, Int64) |
创建在系统内存中具有指定容量的内存映射文件。 |
| CreateNew(String, Int64, MemoryMappedFileAccess) |
在系统内存中创建具有指定容量和访问类型的内存映射文件。 |
| CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability) |
创建具有指定名称、容量、访问类型、内存分配选项和可继承性的内存映射文件。 |
| CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability) |
创建具有指定容量、访问类型、内存分配、安全权限和系统内存中可继承性的内存映射文件。 |
CreateNew(String, Int64)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
创建在系统内存中具有指定容量的内存映射文件。
public:
static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateNew(System::String ^ mapName, long capacity);
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateNew(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
要分配给内存映射文件的名称,或者 null 用于 MemoryMappedFile 不打算跨进程共享的名称。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。
返回
具有指定名称和容量的内存映射文件。
例外
mapName 是空字符串。
capacity 小于或等于零。
仅.NET Core 和 .NET 5+ :仅支持对具有命名内存映射文件的 CreateNew 方法(即非 null mapName)的调用Windows操作系统。
示例
以下示例由将值写入 Boolean 内存映射文件的三个单独的进程(控制台应用程序)组成。 各操作按下面的顺序发生:
- 处理 A 将创建内存映射的文件,并将一个值写入其中。
- 进程 B 将打开内存映射的文件,并将值写入其中。
- 进程 C 将打开内存映射文件,并将值写入其中。
- 处理 A 读取并显示内存映射文件中的值。
- 使用内存映射文件完成进程 A 后,垃圾回收会立即回收该文件。
若要运行此示例,请执行以下作:
- 编译应用程序并打开三个命令窗口。
- 在第一个命令窗口中,运行进程 A。
- 在第二个命令窗口中,运行进程 B。
- 返回到进程 A 并按 Enter。
- 在第三个命令窗口中,运行进程 C。
- 返回到进程 A 并按 Enter。
进程 A 的输出如下所示:
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
进程 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
进程 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
进程 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
注解
使用此方法创建未持久保存的内存映射文件(即未与磁盘上的文件关联),可用于在进程之间共享数据。
另请参阅
适用于
CreateNew(String, Int64, MemoryMappedFileAccess)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
在系统内存中创建具有指定容量和访问类型的内存映射文件。
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);
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
要分配给内存映射文件的名称,或者 null 用于 MemoryMappedFile 不打算跨进程共享的名称。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。
- access
- MemoryMappedFileAccess
枚举值之一,指定允许访问内存映射文件的类型。 默认值为 ReadWrite。
返回
具有指定特征的内存映射文件。
例外
仅.NET Core 和 .NET 5+ :仅支持对具有命名内存映射文件的 CreateNew 方法(即非 null mapName)的调用Windows操作系统。
注解
使用此方法创建未持久保存的内存映射文件(即未与磁盘上的文件关联),可用于在进程之间共享数据。
另请参阅
适用于
CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
创建具有指定名称、容量、访问类型、内存分配选项和可继承性的内存映射文件。
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);
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
要分配给内存映射文件的名称,或者 null 用于 MemoryMappedFile 不打算跨进程共享的名称。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。
- access
- MemoryMappedFileAccess
枚举值之一,指定允许访问内存映射文件的类型。 默认值为 ReadWrite。
- options
- MemoryMappedFileOptions
枚举值的按位组合,指定内存映射文件的内存分配选项。
- inheritability
- HandleInheritability
一个值,该值指定子进程是否可以继承内存映射文件的句柄。 默认值为 None。
返回
具有指定特征的内存映射文件。
例外
capacity 小于或等于零。
-或-
access 不是有效的 MemoryMappedFileAccess 枚举值。
-或-
inheritability 不是有效 HandleInheritability 值。
仅.NET Core 和 .NET 5+ :仅支持对具有命名内存映射文件的 CreateNew 方法(即非 null mapName)的调用Windows操作系统。
适用于
CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)
创建具有指定容量、访问类型、内存分配、安全权限和系统内存中可继承性的内存映射文件。
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);
[<System.Security.SecurityCritical>]
static member CreateNew : string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.MemoryMappedFiles.MemoryMappedFileOptions * System.IO.MemoryMappedFiles.MemoryMappedFileSecurity * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateNew (mapName As String, capacity As Long, access As MemoryMappedFileAccess, options As MemoryMappedFileOptions, memoryMappedFileSecurity As MemoryMappedFileSecurity, inheritability As HandleInheritability) As MemoryMappedFile
参数
- mapName
- String
要分配给内存映射文件的名称,或者 null 用于 MemoryMappedFile 不打算跨进程共享的名称。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。
- access
- MemoryMappedFileAccess
枚举值之一,指定允许访问内存映射文件的类型。 默认值为 ReadWrite。
- options
- MemoryMappedFileOptions
枚举值的按位组合,指定内存映射文件的内存分配选项。
- inheritability
- HandleInheritability
枚举值之一,指定子进程是否可以继承内存映射文件的句柄。 默认值为 None。
返回
具有指定特征的内存映射文件。
- 属性
例外
capacity 小于或等于零。
-或-
access 不是有效的 MemoryMappedFileAccess 枚举值。
-或-
inheritability 不是有效的 HandleInheritability 枚举值。
注解
使用此方法创建未持久保存的内存映射文件(即未与磁盘上的文件关联),可用于在进程之间共享数据。