MemoryMappedFile.OpenExisting 方法

定義

開啟在系統記憶體中現有的具名記憶體對應檔。

多載

OpenExisting(String)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱。

OpenExisting(String, MemoryMappedFileRights)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱和存取權限。

OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱、存取權限和可繼承性。

OpenExisting(String)

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱。

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ OpenExisting(System::String ^ mapName);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName);
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member OpenExisting : string -> System.IO.MemoryMappedFiles.MemoryMappedFile
static member OpenExisting : string -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function OpenExisting (mapName As String) As MemoryMappedFile

參數

mapName
String

記憶體對應檔的名稱。

傳回

有指定之名稱的記憶體對應檔。

屬性

例外狀況

mapNamenull

mapName 為空字串。

針對 mapName 指定的檔案不存在。

範例

開啟保存 Memory-Mapped 檔案

下列範例會開啟名為 ImgA 的記憶體對應檔案,該檔案已經從磁碟上的檔案建立 (,如方法) 的範例 CreateFromFile(String) 所示。

using System;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        // Assumes another process has created the memory-mapped file.
        using (var mmf = MemoryMappedFile.OpenExisting("ImgA"))
        {
            using (var accessor = mmf.CreateViewAccessor(4000000, 2000000))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;

                // Make changes to the view.
                for (long i = 0; i < 1500000; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(20);
                    accessor.Write(i, ref color);
                }
            }
        }
    }
}

public struct MyColor
{
    public short Red;
    public short Green;
    public short Blue;
    public short Alpha;

    // Make the view brigher.
    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.MemoryMappedFiles
Imports System.Runtime.InteropServices

Class Program
    Public Shared Sub Main(ByVal args As String())
        ' Assumes another process has created the memory-mapped file.
        Using mmf = MemoryMappedFile.OpenExisting("ImgA")
            Using accessor = mmf.CreateViewAccessor(4000000, 2000000)
                Dim colorSize As Integer = Marshal.SizeOf(GetType(MyColor))
                Dim color As MyColor

                ' Make changes to the view.
                Dim i As Long = 0
                While i < 1500000
                    accessor.Read(i, color)
                    color.Brighten(30)
                    accessor.Write(i, color)
                    i += colorSize
                End While
            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 brigher.
    Public Sub Brighten(ByVal value As Short)
        Red = CShort(Math.Min(Short.MaxValue, CInt(Red) + value))
        Green = CShort(Math.Min(Short.MaxValue, CInt(Green) + value))
        Blue = CShort(Math.Min(Short.MaxValue, CInt(Blue) + value))
        Alpha = CShort(Math.Min(Short.MaxValue, CInt(Alpha) + value))
    End Sub
End Structure

開啟非保存 Memory-Mapped 檔案

下列範例會開啟用於進程間通訊的記憶體對應檔案。 此程式代碼範例是為 方法提供之較大範例的 CreateNew(String, Int64) 一部分。

備註

記憶體對應檔案可以是與磁碟上) 或非持續性檔案相關聯的持續性記憶體對應檔案 (。

另請參閱

適用於

OpenExisting(String, MemoryMappedFileRights)

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱和存取權限。

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ OpenExisting(System::String ^ mapName, System::IO::MemoryMappedFiles::MemoryMappedFileRights desiredAccessRights);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights);
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights -> System.IO.MemoryMappedFiles.MemoryMappedFile
static member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function OpenExisting (mapName As String, desiredAccessRights As MemoryMappedFileRights) As MemoryMappedFile

參數

mapName
String

要開啟之記憶體對應檔的名稱。

desiredAccessRights
MemoryMappedFileRights

其中一個列舉值,指定要套用至記憶體對應檔的存取權限。

傳回

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

屬性

例外狀況

mapNamenull

mapName 為空字串。

desiredAccessRights 不是有效的 MemoryMappedFileRights 列舉值。

針對 mapName 指定的檔案不存在。

另請參閱

適用於

OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs
來源:
MemoryMappedFile.cs

開啟現有的記憶體對應檔案,此檔案在系統記憶體中具有指定的名稱、存取權限和可繼承性。

public:
 static System::IO::MemoryMappedFiles::MemoryMappedFile ^ OpenExisting(System::String ^ mapName, System::IO::MemoryMappedFiles::MemoryMappedFileRights desiredAccessRights, System::IO::HandleInheritability inheritability);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);
[System.Security.SecurityCritical]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting (string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile
static member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile
[<System.Security.SecurityCritical>]
static member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights * System.IO.HandleInheritability -> System.IO.MemoryMappedFiles.MemoryMappedFile
Public Shared Function OpenExisting (mapName As String, desiredAccessRights As MemoryMappedFileRights, inheritability As HandleInheritability) As MemoryMappedFile

參數

mapName
String

要開啟之記憶體對應檔的名稱。

desiredAccessRights
MemoryMappedFileRights

其中一個列舉值,指定要套用至記憶體對應檔的存取權限。

inheritability
HandleInheritability

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

傳回

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

屬性

例外狀況

mapNamenull

mapName 為空字串。

desiredAccessRights 不是有效的 MemoryMappedFileRights 列舉值。

-或-

inheritability 不是有效的 HandleInheritability 列舉值。

要求的存取是無效的記憶體對應檔案。

針對 mapName 指定的檔案不存在。

另請參閱

適用於