MemoryMappedFile.CreateFromFile 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
基于现有文件创建一个内存映射文件。
重载
| CreateFromFile(String) | 基于磁盘上的文件创建一个内存映射文件。 | 
| CreateFromFile(String, FileMode) | 基于磁盘上的文件创建一个具有指定访问模式的内存映射文件。 | 
| CreateFromFile(String, FileMode, String) | 基于磁盘上的文件创建一个具有指定访问模式和名称的内存映射文件。 | 
| CreateFromFile(String, FileMode, String, Int64) | 基于磁盘上的文件创建一个具有指定访问模式、名称和容量的内存映射文件。 | 
| CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) | 基于磁盘上的文件创建一个具有指定访问模式、名称、容量和访问类型的内存映射文件。 | 
| CreateFromFile(SafeFileHandle, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) | 使用 SafeFileHandle 和指定的访问模式、名称、可继承性和容量从现有文件创建内存映射文件。 | 
| CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) | 从现有文件创建一个具有指定的访问模式、名称、继承性和容量的内存映射文件。 | 
| CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) | 基于磁盘上的文件创建一个具有指定名称、容量、访问类型、安全权限、继承性和释放要求的内存映射文件。 | 
CreateFromFile(String)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
基于磁盘上的文件创建一个内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path);static member CreateFromFile : string -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (path As String) As MemoryMappedFile参数
- path
- String
要映射的文件的路径。
返回
内存映射文件。
例外
              path 为 null。
出现 I/O 错误。
              path 超过了操作系统定义的最大长度。
调用方没有该文件的所需权限。
示例
以下示例使用 CreateFromFile 方法创建内存映射文件,然后创建一个内存映射视图,该视图指向一个极大型文件的一部分。
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
class Program
{
    static void Main(string[] args)
    {
        long offset = 0x10000000; // 256 megabytes
        long length = 0x20000000; // 512 megabytes
        // Create the memory-mapped file.
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
        {
            // Create a random access view, from the 256th megabyte (the offset)
            // to the 768th megabyte (the offset plus length).
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;
                // Make changes to the view.
                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(10);
                    accessor.Write(i, ref color);
                }
            }
        }
    }
}
public struct MyColor
{
    public short Red;
    public short Green;
    public short Blue;
    public short Alpha;
    // Make the view brighter.
    public void Brighten(short value)
    {
        Red = (short)Math.Min(short.MaxValue, (int)Red + value);
        Green = (short)Math.Min(short.MaxValue, (int)Green + value);
        Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
        Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    }
}
Imports System.IO
Imports System.IO.MemoryMappedFiles
Imports System.Runtime.InteropServices
Class Program
    Sub Main()
        Dim offset As Long = &H10000000 ' 256 megabytes
        Dim length As Long = &H20000000 ' 512 megabytes
        ' Create the memory-mapped file.
        Using mmf = MemoryMappedFile.CreateFromFile("c:\ExtremelyLargeImage.data", FileMode.Open, "ImgA")
            ' Create a random access view, from the 256th megabyte (the offset)
            ' to the 768th megabyte (the offset plus length).
            Using accessor = mmf.CreateViewAccessor(offset, length)
                Dim colorSize As Integer = Marshal.SizeOf(GetType(MyColor))
                Dim color As MyColor
                Dim i As Long = 0
                ' Make changes to the view.
                Do While (i < length)
                    accessor.Read(i, color)
                    color.Brighten(10)
                    accessor.Write(i, color)
                    i += colorSize
                Loop
            End Using
        End Using
    End Sub
End Class
Public Structure MyColor
    Public Red As Short
    Public Green As Short
    Public Blue As Short
    Public Alpha As Short
    ' Make the view brighter.
    Public Sub Brighten(ByVal value As Short)
        Red = CType(Math.Min(Short.MaxValue, (CType(Red, Integer) + value)), Short)
        Green = CType(Math.Min(Short.MaxValue, (CType(Green, Integer) + value)), Short)
        Blue = CType(Math.Min(Short.MaxValue, (CType(Blue, Integer) + value)), Short)
        Alpha = CType(Math.Min(Short.MaxValue, (CType(Alpha, Integer) + value)), Short)
    End Sub
End Structure
另请参阅
适用于
CreateFromFile(String, FileMode)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
基于磁盘上的文件创建一个具有指定访问模式的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path, System::IO::FileMode mode);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode);static member CreateFromFile : string * System.IO.FileMode -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (path As String, mode As FileMode) As MemoryMappedFile参数
- path
- String
要映射的文件的路径。
返回
具有指定访问模式的内存映射文件。
例外
              path 为空字符串,只包含空格,或者包含 GetInvalidFileNameChars() 方法定义的一个或多个无效字符。
- 或 -
              path 引用无效的设备。
- 或 -
              mode 为 Append。
              path 为 null。
              path 超过了操作系统定义的最大长度。
调用方没有该文件的所需权限。
注解
参数 mode 与磁盘上的源文件相关。 只能使用 Open 枚举值从磁盘上的源文件创建内存映射文件。
另请参阅
适用于
CreateFromFile(String, FileMode, String)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
基于磁盘上的文件创建一个具有指定访问模式和名称的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path, System::IO::FileMode mode, System::String ^ mapName);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string? mapName);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string mapName);static member CreateFromFile : string * System.IO.FileMode * string -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String) As MemoryMappedFile参数
- path
- String
要映射的文件的路径。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
返回
具有指定名称和访问模式的内存映射文件。
例外
              path 为空字符串,只包含空格,或者包含 GetInvalidFileNameChars() 方法定义的一个或多个无效字符。
- 或 -
              path 引用无效的设备。
- 或 -
              mapName 是一个空字符串。
或
              mode 为 Append。
              path 为 null。
              path 超过了操作系统定义的最大长度。
调用方没有该文件的所需权限。
注解
参数 mode 与磁盘上的源文件相关。 只能使用 Open 枚举值从磁盘上的源文件创建内存映射文件。
适用于
CreateFromFile(String, FileMode, String, Int64)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
基于磁盘上的文件创建一个具有指定访问模式、名称和容量的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path, System::IO::FileMode mode, System::String ^ mapName, long capacity);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string? mapName, long capacity);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string mapName, long capacity);static member CreateFromFile : string * System.IO.FileMode * string * int64 -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long) As MemoryMappedFile参数
- path
- String
要映射的文件的路径。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。 指定 0,以将容量设置为磁盘上文件的大小。
返回
具有指定特征的内存映射文件。
例外
              path 为空字符串,只包含空格,或者包含 GetInvalidFileNameChars() 方法定义的一个或多个无效字符。
- 或 -
              path 引用无效的设备。
- 或 -
              mapName 是一个空字符串。
或
              mode 为 Append。
              path 为 null。
              capacity 大于逻辑地址空间的大小。
- 或 -
              capacity 小于零。
- 或 -
              capacity 小于文件大小(但不为零)。
- 或 -
              capacity 为零,且在磁盘上文件的大小也为零。
出现 I/O 错误。
              path 超过了操作系统定义的最大长度。
调用方没有该文件的所需权限。
注解
参数 mode 与磁盘上的源文件相关。
如果 capacity 大于磁盘上文件的大小,则磁盘上的文件将增加以匹配指定的容量,即使没有数据写入内存映射文件也是如此。 若要防止发生这种情况,请为默认容量指定 0 (零) ,这将在内部设置为 capacity 磁盘上的文件大小。
适用于
CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
基于磁盘上的文件创建一个具有指定访问模式、名称、容量和访问类型的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path, System::IO::FileMode mode, System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string? mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);[System.Security.SecurityCritical]
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (string path, System.IO.FileMode mode, string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access);static member CreateFromFile : string * System.IO.FileMode * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess -> System.IO.MemoryMappedFiles.MemoryMappedFile[<System.Security.SecurityCritical>]
static member CreateFromFile : string * System.IO.FileMode * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long, access As MemoryMappedFileAccess) As MemoryMappedFile参数
- path
- String
要映射的文件的路径。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。 指定 0,以将容量设置为磁盘上文件的大小。
- access
- MemoryMappedFileAccess
指定内存映射文件允许的访问类型的枚举值之一。
返回
具有指定特征的内存映射文件。
- 属性
例外
              mapName 是一个空字符串。
- 或 -
              access 不是一个允许的值。
- 或 -
              path 指定空的文件。
- 或 -
              access 指定为 Read,容量大于 path 所指示文件的大小。
- 或 -
              mode 为 Append。
              path 为 null。
              capacity 大于逻辑地址空间的大小。
- 或 -
              capacity 小于零。
- 或 -
              capacity 小于文件大小(但不为零)。
- 或 -
              capacity 为零,且在磁盘上文件的大小也为零。
- 或 -
              access 不是定义的 MemoryMappedFileAccess 值。
- 或 -
              path 指示的文件大小大于 capacity。
              path 超过了操作系统定义的最大长度。
调用方没有该文件的所需权限。
注解
参数 mode 与磁盘上的源文件相关。
如果 capacity 大于磁盘上文件的大小,则磁盘上的文件将增加以匹配指定的容量,即使没有数据写入内存映射文件也是如此。 若要防止发生这种情况,请为默认容量指定 0 (零) ,这将在内部设置为 capacity 磁盘上的文件大小。
另请参阅
适用于
CreateFromFile(SafeFileHandle, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
使用 SafeFileHandle 和指定的访问模式、名称、可继承性和容量从现有文件创建内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access, System::IO::HandleInheritability inheritability, bool leaveOpen);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, string? mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.HandleInheritability inheritability, bool leaveOpen);static member CreateFromFile : Microsoft.Win32.SafeHandles.SafeFileHandle * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.HandleInheritability * bool -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (fileHandle As SafeFileHandle, mapName As String, capacity As Long, access As MemoryMappedFileAccess, inheritability As HandleInheritability, leaveOpen As Boolean) As MemoryMappedFile参数
- fileHandle
- SafeFileHandle
现有 SafeFileHandle 文件的 。 当 (由) 自动释放 fileHandle 时 leaveOpentrue ,调用方负责释放 MemoryMappedFile 。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。 指定 0 将容量设置为文件大小。
- inheritability
- HandleInheritability
指定内存映射文件的句柄能否由子进程继承的枚举值之一。 默认值为 None。
- leaveOpen
- Boolean
一个 值,该值指示是否在释放 时 MemoryMappedFile 关闭源文件句柄。
返回
具有指定特征的内存映射文件。
例外
  
              mapName 为 null 或空字符串。
- 或 -
  
              capacity 和文件长度为零。
- 或 -
  
              access 设置为 Write,这是不允许的。
-或-
  
              access 设置为 Read ,大于 capacity 文件的长度。
              fileHandle 为 null。
  
              capacity 小于零。
- 或 -
  
              capacity 小于文件大小。
- 或 -
  
              access 不是有效的 MemoryMappedFileAccess 枚举值。
- 或 -
  
              inheritability 不是有效的 HandleInheritability 枚举值。
适用于
CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
从现有文件创建一个具有指定的访问模式、名称、继承性和容量的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::IO::FileStream ^ fileStream, System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access, System::IO::HandleInheritability inheritability, bool leaveOpen);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (System.IO.FileStream fileStream, string? mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.HandleInheritability inheritability, bool leaveOpen);public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (System.IO.FileStream fileStream, string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.HandleInheritability inheritability, bool leaveOpen);static member CreateFromFile : System.IO.FileStream * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.HandleInheritability * bool -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (fileStream As FileStream, mapName As String, capacity As Long, access As MemoryMappedFileAccess, inheritability As HandleInheritability, leaveOpen As Boolean) As MemoryMappedFile参数
- fileStream
- FileStream
现有文件的文件流。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。 指定 0 将容量设置为 的大小 filestream。
- inheritability
- HandleInheritability
指定内存映射文件的句柄能否由子进程继承的枚举值之一。 默认值为 None。
- leaveOpen
- Boolean
一个值,该值指示释放 MemoryMappedFile 后是否要关闭源文件流。
返回
具有指定特征的内存映射文件。
例外
              mapName 为 null 或空字符串。
- 或 -
              capacity 和文件长度为零。
- 或 -
              access 设置为 Write 或 Write 枚举值,这是不允许的。
- 或 -
              access 设置为 Read,capacity 大于 filestream 的长度。
              fileStream 为 null。
              capacity 小于零。
- 或 -
              capacity 小于文件大小。
- 或 -
              access 不是有效的 MemoryMappedFileAccess 枚举值。
- 或 -
              inheritability 不是有效的 HandleInheritability 枚举值。
适用于
CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)
基于磁盘上的文件创建一个具有指定名称、容量、访问类型、安全权限、继承性和释放要求的内存映射文件。
public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::IO::FileStream ^ fileStream, System::String ^ mapName, long capacity, System::IO::MemoryMappedFiles::MemoryMappedFileAccess access, System::IO::MemoryMappedFiles::MemoryMappedFileSecurity ^ memoryMappedFileSecurity, System::IO::HandleInheritability inheritability, bool leaveOpen);[System.Security.SecurityCritical]
public static System.IO.MemoryMappedFiles.MemoryMappedFile CreateFromFile (System.IO.FileStream fileStream, string mapName, long capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess access, System.IO.MemoryMappedFiles.MemoryMappedFileSecurity memoryMappedFileSecurity, System.IO.HandleInheritability inheritability, bool leaveOpen);[<System.Security.SecurityCritical>]
static member CreateFromFile : System.IO.FileStream * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.MemoryMappedFiles.MemoryMappedFileSecurity * System.IO.HandleInheritability * bool -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function CreateFromFile (fileStream As FileStream, mapName As String, capacity As Long, access As MemoryMappedFileAccess, memoryMappedFileSecurity As MemoryMappedFileSecurity, inheritability As HandleInheritability, leaveOpen As Boolean) As MemoryMappedFile参数
- fileStream
- FileStream
要映射的文件的 fileStream。
- mapName
- String
要分配给内存映射文件的名称,或用于你不打算在进程中共享的 MemoryMappedFile 的 null。
- capacity
- Int64
要分配给内存映射文件的最大大小(以字节为单位)。 指定 0,以将容量设置为磁盘上文件的大小。
- inheritability
- HandleInheritability
指定内存映射文件的句柄能否由子进程继承的枚举值之一。 默认值为 None。
- leaveOpen
- Boolean
若为 true,则在关闭 MemoryMappedFile 后不释放 fileStream;若为 false,则释放 fileStream。
返回
具有指定特征的内存映射文件。
- 属性
例外
              mapName 是一个空字符串。
- 或 -
              capacity 和文件长度为零。
- 或 -
              fileStream 为 null。
              capacity 小于零。
- 或 -
              capacity 小于文件大小。
- 或 -
              access 不是有效的 MemoryMappedFileAccess 枚举值。
- 或 -
              inheritability 不是有效的 HandleInheritability 枚举值。
              fileStream 已关闭。
              mapName 已存在。
注解
如果 capacity 大于磁盘上文件的大小,则磁盘上的文件将增加以匹配指定的容量,即使没有数据写入内存映射文件也是如此。 若要防止发生这种情况,请为默认容量指定 0 (零) ,这将在内部设置为 capacity 磁盘上的文件大小。