MemoryMappedFile.CreateFromFile 方法

定義

從現有檔案建立記憶體對應檔。

多載

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)

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

參數

path
String

要對應之檔案的路徑。

傳回

記憶體對應檔。

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。

-或-

path 是指不正確的裝置。

pathnull

發生 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)

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

參數

path
String

要對應之檔案的路徑。

mode
FileMode

存取模式;必須是 Open

傳回

記憶體對應檔,具有指定的存取模式。

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。

-或-

path 是指不正確的裝置。

-或-

modeAppend

pathnull

modeCreateCreateNewTruncate

-或-

modeOpenOrCreate,且磁碟中的檔案不存在。

-或-

發生 I/O 錯誤。

path 超過作業系統定義的長度上限。

呼叫端沒有所需的檔案權限。

備註

參數 mode 與磁碟上的來源檔案有關。 您只能 Open 使用 列舉值,從磁碟上的來源檔案建立記憶體對應檔案。

另請參閱

適用於

CreateFromFile(String, FileMode, String)

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

參數

path
String

要對應之檔案的路徑。

mode
FileMode

存取模式;必須是 Open

mapName
String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 null

傳回

記憶體對應檔,具有指定的名稱和存取模式。

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。

-或-

path 是指不正確的裝置。

-或-

mapName 為空字串。

-或-

modeAppend

pathnull

modeCreateCreateNewTruncate

-或-

modeOpenOrCreate,且磁碟中的檔案不存在。

-或-

發生 I/O 錯誤。

path 超過作業系統定義的長度上限。

呼叫端沒有所需的檔案權限。

備註

參數 mode 與磁碟上的來源檔案有關。 您只能 Open 使用 列舉值,從磁碟上的來源檔案建立記憶體對應檔案。

適用於

CreateFromFile(String, FileMode, String, Int64)

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
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.MemoryMappedFile
Public Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long) As MemoryMappedFile

參數

path
String

要對應之檔案的路徑。

mode
FileMode

存取模式,可以是除了 FileMode 之外的任何 Append 列舉值。

mapName
String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 null

capacity
Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。 指定 0 會將大小設定為磁碟上檔案的大小。

傳回

記憶體對應檔,具有指定的特性。

例外狀況

path 為空字串,只包含空白字元或包含一個以上 GetInvalidFileNameChars() 所定義的無效字元。

-或-

path 是指不正確的裝置。

-或-

mapName 為空字串。

-或-

modeAppend

pathnull

capacity 大於邏輯位址空間的大小。

-或-

capacity 小於零。

-或-

capacity 小於檔案大小 (但不為零)。

-或-

capacity 是零,而且磁碟上檔案的大小也是零。

發生 I/O 錯誤。

path 超過作業系統定義的長度上限。

呼叫端沒有所需的檔案權限。

備註

參數 mode 與磁碟上的來源檔案有關。

如果 capacity 大於磁碟上的檔案大小,磁碟上的檔案會增加以符合指定的容量,即使沒有數據寫入記憶體對應檔案也一樣。 若要避免發生這種情況,請針對預設容量指定 0 (零) ,這會在內部設定 capacity 為磁碟上的檔案大小。

適用於

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

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
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.MemoryMappedFile
Public Shared Function CreateFromFile (path As String, mode As FileMode, mapName As String, capacity As Long, access As MemoryMappedFileAccess) As MemoryMappedFile

參數

path
String

要對應之檔案的路徑。

mode
FileMode

存取模式,可以是除了 FileMode 之外的任何 Append 列舉值。

mapName
String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 null

capacity
Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。 指定 0 會將大小設定為磁碟上檔案的大小。

access
MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。

傳回

記憶體對應檔,具有指定的特性。

屬性

例外狀況

mapName 為空字串。

-或-

access 不是允許的值。

-或-

path 會指定空檔案。

-或-

access 已指定為 Read,而且容量大於 path 所指示的檔案大小。

-或-

modeAppend

pathnull

capacity 大於邏輯位址空間的大小。

-或-

capacity 小於零。

-或-

capacity 小於檔案大小 (但不為零)。

-或-

capacity 是零,而且磁碟上檔案的大小也是零。

-或-

access 不是已定義的 MemoryMappedFileAccess 值。

-或-

path 所指示的檔案大小大於 capacity

-或-

發生 I/O 錯誤。

path 超過作業系統定義的長度上限。

呼叫端沒有所需的檔案權限。

備註

參數 mode 與磁碟上的來源檔案有關。

如果 capacity 大於磁碟上的檔案大小,磁碟上的檔案會增加以符合指定的容量,即使沒有數據寫入記憶體對應檔案也一樣。 若要避免發生這種情況,請針對預設容量指定 0 (零) ,這會在內部設定 capacity 為磁碟上的檔案大小。

另請參閱

適用於

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

來源:
MemoryMappedFile.cs
來源:
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.MemoryMappedFile
Public 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 檔案的 。 呼叫端負責處置 fileHandleleaveOpentrue (,否則會自動由 MemoryMappedFile) 處置。

mapName
String

要指派給記憶體對應檔的名稱;針對您不想要跨處理序共用的 MemoryMappedFile,則為 null

capacity
Int64

要配置給記憶體對應檔的大小上限 (以位元組為單位)。 指定 0 將容量設定為檔案的大小。

access
MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。

此參數無法設定為 Write

inheritability
HandleInheritability

其中一個列舉值,決定記憶體對應檔的控制代碼是否可以由子處理序繼承。 預設為 None

leaveOpen
Boolean

值,指出是否要在處置 時 MemoryMappedFile 關閉原始程序檔句柄。

傳回

記憶體對應檔,具有指定的特性。

例外狀況

mapNamenull 或空字串。

-或-

檔案的 capacity 和長度都是零。

-或-

access 設定為 Write,不允許。

-或-

access 設定為 Read ,且 capacity 大於檔案的長度。

fileHandlenull

capacity 小於零。

-或-

capacity 小於檔案大小。

-或-

access 不是有效的 MemoryMappedFileAccess 列舉值。

-或-

inheritability 不是有效的 HandleInheritability 列舉值。

適用於

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

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
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.MemoryMappedFile
Public 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

access
MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。

此參數無法設定為 Write

inheritability
HandleInheritability

其中一個列舉值,決定記憶體對應檔的控制代碼是否可以由子處理序繼承。 預設為 None

leaveOpen
Boolean

表示是否要在處置 MemoryMappedFile 時,關閉來源檔案資料流的值。

傳回

記憶體對應檔,具有指定的特性。

例外狀況

mapNamenull 或空字串。

-或-

檔案的 capacity 和長度都是零。

-或-

access 設定為 Write,或 Write 列舉值表示不允許。

-或-

access 設定為 Read,而 capacity 大於 filestream 的長度。

fileStreamnull

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.MemoryMappedFile
Public 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 會將大小設定為磁碟上檔案的大小。

access
MemoryMappedFileAccess

其中一個列舉值,指定記憶體對應檔允許的存取類型。

此參數無法設定為 Write

memoryMappedFileSecurity
MemoryMappedFileSecurity

允許對記憶體對應檔進行檔案存取及作業的可授與權限。

這個參數可以是 null

inheritability
HandleInheritability

其中一個列舉值,決定記憶體對應檔的控制代碼是否可以由子處理序繼承。 預設為 None

leaveOpen
Boolean

true 表示不會在 MemoryMappedFile 關閉後處置 fileStreamfalse 則處置 fileStream

傳回

記憶體對應檔,具有指定的特性。

屬性

例外狀況

mapName 為空字串。

-或-

檔案的 capacity 和長度都是零。

-或-

access 會設定為 ReadWrite 列舉值,即表示不允許。

fileStreamnull

capacity 小於零。

-或-

capacity 小於檔案大小。

-或-

access 不是有效的 MemoryMappedFileAccess 列舉值。

-或-

inheritability 不是有效的 HandleInheritability 列舉值。

fileStream 已關閉。

access 的存取權設定為 ReadWriteRead 時,fileStream 會設定為 Write

mapName 已經存在。

備註

如果 capacity 大於磁碟上的檔案大小,磁碟上的檔案會增加以符合指定的容量,即使沒有數據寫入記憶體對應檔案也一樣。 若要避免發生這種情況,請針對預設容量指定 0 (零) ,這會在內部設定 capacity 為磁碟上的檔案大小。

另請參閱

適用於