MemoryMappedFile.CreateFromFile MemoryMappedFile.CreateFromFile MemoryMappedFile.CreateFromFile MemoryMappedFile.CreateFromFile Method

定義

從現有檔案建立記憶體對應檔。Creates a memory-mapped file from an existing file.

多載

CreateFromFile(String) CreateFromFile(String) CreateFromFile(String) CreateFromFile(String)

從磁碟上的檔案建立記憶體對應檔。Creates a memory-mapped file from a file on disk.

CreateFromFile(String, FileMode) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式。Creates a memory-mapped file that has the specified access mode from a file on disk.

CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式和名稱。Creates a memory-mapped file that has the specified access mode and name from a file on disk.

CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱和大小。Creates a memory-mapped file that has the specified access mode, name, and capacity from a file on disk.

CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱、大小和存取類型。Creates a memory-mapped file that has the specified access mode, name, capacity, and access type from a file on disk.

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)

從現有的檔案建立記憶體對應檔,這個檔案具有指定的存取模式、名稱、可繼承性和容量。Creates a memory-mapped file from an existing file with the specified access mode, name, inheritability, and capacity.

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)

從磁碟上的檔案建立記憶體對應檔案,這個記憶體對應檔具有指定的名稱、大小、存取類型、安全性權限、可繼承性和處置需求。Creates a memory-mapped file that has the specified name, capacity, access type, security permissions, inheritability, and disposal requirement from a file on disk.

CreateFromFile(String) CreateFromFile(String) CreateFromFile(String) CreateFromFile(String)

從磁碟上的檔案建立記憶體對應檔。Creates a memory-mapped file from a file on disk.

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.MemoryMappedFile
Public Shared Function CreateFromFile (path As String) As MemoryMappedFile

參數

path
String String String String

要對應之檔案的路徑。The path to file to map.

傳回

記憶體對應檔。A memory-mapped file.

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。path is an empty string, contains only white space, or has one or more invalid characters, as defined by the GetInvalidFileNameChars() method.

-或--or- path 是指不正確的裝置。path refers to an invalid device.

發生 I/O 錯誤。An I/O error occurred.

path 超過作業系統定義的長度上限。path exceeds the maximum length defined by the operating system.

呼叫端沒有所需的檔案權限。The caller does not have the required permissions for the file.

範例

下列範例會使用CreateFromFile方法來建立記憶體對應檔案, 然後再建立記憶體對應的視圖, 以成為極大檔案的一部分。The following example uses the CreateFromFile method to create a memory-mapped file, and then creates a memory-mapped view to a portion of an extremely large file.

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) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode) CreateFromFile(String, FileMode)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式。Creates a memory-mapped file that has the specified access mode from a file on disk.

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.MemoryMappedFile
Public Shared Function CreateFromFile (path As String, mode As FileMode) As MemoryMappedFile

參數

path
String String String String

要對應之檔案的路徑。The path to the file to map.

mode
FileMode FileMode FileMode FileMode

存取模式;必須是 OpenAccess mode; must be Open.

傳回

記憶體對應檔,具有指定的存取模式。A memory-mapped file that has the specified access mode.

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。path is an empty string, contains only white space, or has one or more invalid characters, as defined by the GetInvalidFileNameChars() method.

-或--or- path 是指不正確的裝置。path refers to an invalid device.

-或--or- modeAppendmode is Append.

modeCreateCreateNewTruncatemode is Create, CreateNew, or Truncate.

-或--or- modeOpenOrCreate,且磁碟中的檔案不存在。mode is OpenOrCreate and the file on disk does not exist.

-或--or- 發生 I/O 錯誤。An I/O error occurred.

path 超過作業系統定義的長度上限。path exceeds the maximum length defined by the operating system.

呼叫端沒有所需的檔案權限。The caller does not have the required permissions for the file.

備註

mode參數與磁片上的來源檔案有關。The mode parameter pertains to the source file on disk. 您只能使用Open列舉值, 從磁片上的來源檔案建立記憶體對應檔案。You can use only the Open enumeration value to create the memory-mapped file from the source file on disk.

另請參閱

CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String) CreateFromFile(String, FileMode, String)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式和名稱。Creates a memory-mapped file that has the specified access mode and name from a file on disk.

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);
static member CreateFromFile : string * System.IO.FileMode * string -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String) As MemoryMappedFile

參數

path
String String String String

要對應之檔案的路徑。The path to the file to map.

mode
FileMode FileMode FileMode FileMode

存取模式;必須是 OpenAccess mode; must be Open.

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.

傳回

記憶體對應檔,具有指定的名稱和存取模式。A memory-mapped file that has the specified name and access mode.

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。path is an empty string, contains only white space, or has one or more invalid characters, as defined by the GetInvalidFileNameChars() method.

-或--or- path 是指不正確的裝置。path refers to an invalid device.

-或--or- mapName 為空字串。mapName is an empty string.

-或--or- modeAppendmode is Append.

modeCreateCreateNewTruncatemode is Create, CreateNew, or Truncate.

-或--or- modeOpenOrCreate,且磁碟中的檔案不存在。mode is OpenOrCreate and the file on disk does not exist.

-或--or- 發生 I/O 錯誤。An I/O error occurred.

path 超過作業系統定義的長度上限。path exceeds the maximum length defined by the operating system.

呼叫端沒有所需的檔案權限。The caller does not have the required permissions for the file.

備註

mode參數與磁片上的來源檔案有關。The mode parameter pertains to the source file on disk. 您只能使用Open列舉值, 從磁片上的來源檔案建立記憶體對應檔案。You can use only the Open enumeration value to create the memory-mapped file from the source file on disk.

CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64) CreateFromFile(String, FileMode, String, Int64)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱和大小。Creates a memory-mapped file that has the specified access mode, name, and capacity from a file on disk.

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);
static member CreateFromFile : string * System.IO.FileMode * string * int64 -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long) As MemoryMappedFile

參數

path
String String String String

要對應之檔案的路徑。The path to the file to map.

mode
FileMode FileMode FileMode FileMode

存取模式,可以是除了 FileMode 之外的任何 Append 列舉值。Access mode; can be any of the FileMode enumeration values except Append.

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. 指定 0 會將大小設定為磁碟上檔案的大小。Specify 0 to set the capacity to the size of the file on disk.

傳回

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

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。path is an empty string, contains only white space, or has one or more invalid characters, as defined by the GetInvalidFileNameChars() method.

-或--or- path 是指不正確的裝置。path refers to an invalid device.

-或--or- mapName 為空字串。mapName is an empty string.

-或--or- modeAppendmode is Append.

capacity 大於邏輯位址空間的大小。capacity is greater than the size of the logical address space.

-或--or- capacity 小於零。capacity is less than zero.

-或--or- capacity 小於檔案大小 (但不為零)。capacity is less than the file size (but not zero).

-或--or- capacity 是零,而且磁碟上檔案的大小也是零。capacity is zero, and the size of the file on disk is also zero.

發生 I/O 錯誤。An I/O error occurred.

path 超過作業系統定義的長度上限。path exceeds the maximum length defined by the operating system.

呼叫端沒有所需的檔案權限。The caller does not have the required permissions for the file.

備註

mode參數與磁片上的來源檔案有關。The mode parameter pertains to the source file on disk.

如果capacity大於磁片上的檔案大小, 即使沒有任何資料寫入記憶體對應檔, 磁片上的檔案也會增加以符合指定的容量。If capacity is larger than the size of the file on disk, the file on disk is increased to match the specified capacity even if no data is written to the memory-mapped file. 為避免發生這種情況, 請指定 0 (零) 做為預設容量, 這會capacity在內部設定為磁片上的檔案大小。To prevent this from occurring, specify 0 (zero) for the default capacity, which will internally set capacity to the size of the file on disk.

CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)

從磁碟上的檔案建立記憶體對應檔,這個記憶體對應檔具有指定的存取模式、名稱、大小和存取類型。Creates a memory-mapped file that has the specified access mode, name, capacity, and access type from a file on disk.

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ CreateFromFile(System::String ^ path, System::IO::FileMode mode, System::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
Public Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long, access As MemoryMappedFileAccess) As MemoryMappedFile

參數

path
String String String String

要對應之檔案的路徑。The path to the file to map.

mode
FileMode FileMode FileMode FileMode

存取模式,可以是除了 FileMode 之外的任何 Append 列舉值。Access mode; can be any of the FileMode enumeration values except Append.

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. 指定 0 會將大小設定為磁碟上檔案的大小。Specify 0 to set the capacity to the size of the file on disk.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

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

傳回

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

例外狀況

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

-或--or- access 不是允許的值。access is not an allowed value.

-或--or- path 會指定空檔案。path specifies an empty file.

-或--or- access 已指定為 Read,而且容量大於 path 所指示的檔案大小。access is specified as Read and capacity is greater than the size of the file indicated by path.

-或--or- modeAppendmode is Append.

capacity 大於邏輯位址空間的大小。capacity is greater than the size of the logical address space.

-或--or- capacity 小於零。capacity is less than zero.

-或--or- capacity 小於檔案大小 (但不為零)。capacity is less than the file size (but not zero).

-或--or- capacity 是零,而且磁碟上檔案的大小也是零。capacity is zero, and the size of the file on disk is also zero.

-或--or- access 不是已定義的 MemoryMappedFileAccess 值。access is not a defined MemoryMappedFileAccess value.

-或--or- path 所指示的檔案大小大於 capacityThe size of the file indicated by path is greater than capacity.

-或--or-

發生 I/O 錯誤。An I/O error occurred.

path 超過作業系統定義的長度上限。path exceeds the maximum length defined by the operating system.

呼叫端沒有所需的檔案權限。The caller does not have the required permissions for the file.

備註

mode參數與磁片上的來源檔案有關。The mode parameter pertains to the source file on disk.

如果capacity大於磁片上的檔案大小, 即使沒有任何資料寫入記憶體對應檔, 磁片上的檔案也會增加以符合指定的容量。If capacity is larger than the size of the file on disk, the file on disk is increased to match the specified capacity even if no data is written to the memory-mapped file. 為避免發生這種情況, 請指定 0 (零) 做為預設容量, 這會capacity在內部設定為磁片上的檔案大小。To prevent this from occurring, specify 0 (zero) for the default capacity, which will internally set capacity to the size of the file on disk.

安全性

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

另請參閱

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)

從現有的檔案建立記憶體對應檔,這個檔案具有指定的存取模式、名稱、可繼承性和容量。Creates a memory-mapped file from an existing file with the specified access mode, name, inheritability, and capacity.

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);
static member CreateFromFile : System.IO.FileStream * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.HandleInheritability * bool -> System.IO.MemoryMappedFiles.MemoryMappedFile

參數

fileStream
FileStream FileStream FileStream FileStream

現有檔案的檔案資料流。The file stream of the existing file.

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. 指定 0 會將容量設定為 filestream 的大小。Specify 0 to set the capacity to the size of filestream.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

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

此參數無法設定為 WriteThis parameter can't be set to Write.

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.

leaveOpen
Boolean Boolean Boolean Boolean

表示是否要在處置 MemoryMappedFile 時,關閉來源檔案資料流的值。A value that indicates whether to close the source file stream when the MemoryMappedFile is disposed.

傳回

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

例外狀況

mapNamenull 或空字串。mapName is null or an empty string.

-或--or- 檔案的 capacity 和長度都是零。capacity and the length of the file are zero.

-或--or- access 設定為 Write,或 Write 列舉值表示不允許。access is set to Write or Write enumeration value, which is not allowed.

-或--or- access 設定為 Read,而 capacity 大於 filestream 的長度。access is set to Read and capacity is larger than the length of filestream.

capacity 小於零。capacity is less than zero.

-或--or- capacity 小於檔案大小。capacity is less than the file size.

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

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

CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)

從磁碟上的檔案建立記憶體對應檔案,這個記憶體對應檔具有指定的名稱、大小、存取類型、安全性權限、可繼承性和處置需求。Creates a memory-mapped file that has the specified name, capacity, access type, security permissions, inheritability, and disposal requirement from a file on disk.

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);
static member CreateFromFile : System.IO.FileStream * string * int64 * System.IO.MemoryMappedFiles.MemoryMappedFileAccess * System.IO.MemoryMappedFiles.MemoryMappedFileSecurity * System.IO.HandleInheritability * bool -> System.IO.MemoryMappedFiles.MemoryMappedFile

參數

fileStream
FileStream FileStream FileStream FileStream

要對應的檔案的 fileStreamThe fileStream to the file to map.

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. 指定 0 會將大小設定為磁碟上檔案的大小。Specify 0 to set the capacity to the size of the file on disk.

access
MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess MemoryMappedFileAccess

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

此參數無法設定為 WriteThis parameter can't be set to Write.

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.

leaveOpen
Boolean Boolean Boolean Boolean

true 表示不會在 MemoryMappedFile 關閉後處置 fileStreamfalse 則處置 fileStreamtrue to not dispose fileStream after the MemoryMappedFile is closed; false to dispose fileStream.

傳回

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

例外狀況

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

-或--or- 檔案的 capacity 和長度都是零。capacity and the length of the file are zero.

-或--or- access 會設定為 ReadWrite 列舉值,即表示不允許。access is set to the Read or Write enumeration value, which is not allowed.

capacity 小於零。capacity is less than zero.

-或--or- capacity 小於檔案大小。capacity is less than the file size.

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

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

access 的存取權設定為 ReadWriteRead 時,fileStream 會設定為 Writeaccess is set to ReadWrite when fileStream's access is set to Read or Write.

mapName 已經存在。mapName already exists.

備註

如果capacity大於磁片上的檔案大小, 即使沒有任何資料寫入記憶體對應檔, 磁片上的檔案也會增加以符合指定的容量。If capacity is larger than the size of the file on disk, the file on disk is increased to match the specified capacity even if no data is written to the memory-mapped file. 為避免發生這種情況, 請指定 0 (零) 做為預設容量, 這會capacity在內部設定為磁片上的檔案大小。To prevent this from occurring, specify 0 (zero) for the default capacity, which will internally set capacity to the size of the file on disk.

安全性

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

另請參閱

適用於