MemoryMappedFile.OpenExisting 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在系统内存中打开现有的已命名内存映射文件。
重载
| OpenExisting(String) | 在系统内存中打开一个具有指定名称的现有内存映射文件。 | 
| OpenExisting(String, MemoryMappedFileRights) | 在系统内存中打开一个具有指定名称和访问权限的现有内存映射文件。 | 
| OpenExisting(String, MemoryMappedFileRights, HandleInheritability) | 在系统内存中打开一个具有指定名称、访问权限和继承性的现有内存映射文件。 | 
OpenExisting(String)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- 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.MemoryMappedFilestatic member OpenExisting : string -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function OpenExisting (mapName As String) As MemoryMappedFile参数
- mapName
- String
内存映射文件的名称。
返回
具有指定名称的内存映射文件。
- 属性
例外
              mapName 为 null。
              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)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- 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.MemoryMappedFilestatic member OpenExisting : string * System.IO.MemoryMappedFiles.MemoryMappedFileRights -> System.IO.MemoryMappedFiles.MemoryMappedFilePublic Shared Function OpenExisting (mapName As String, desiredAccessRights As MemoryMappedFileRights) As MemoryMappedFile参数
- mapName
- String
要打开的内存映射文件的名称。
- desiredAccessRights
- MemoryMappedFileRights
指定要应用于内存映射文件的访问权限的枚举值之一。
返回
具有指定特征的内存映射文件。
- 属性
例外
              mapName 为 null。
              mapName 是一个空字符串。
              desiredAccessRights 不是有效的 MemoryMappedFileRights 枚举值。
为 mapName 指定的文件不存在。
另请参阅
适用于
OpenExisting(String, MemoryMappedFileRights, HandleInheritability)
- Source:
- MemoryMappedFile.cs
- Source:
- MemoryMappedFile.cs
- Source:
- 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.MemoryMappedFilestatic 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.MemoryMappedFilePublic Shared Function OpenExisting (mapName As String, desiredAccessRights As MemoryMappedFileRights, inheritability As HandleInheritability) As MemoryMappedFile参数
- mapName
- String
要打开的内存映射文件的名称。
- desiredAccessRights
- MemoryMappedFileRights
指定要应用于内存映射文件的访问权限的枚举值之一。
- inheritability
- HandleInheritability
指定内存映射文件的句柄能否由子进程继承的枚举值之一。 默认值为 None。
返回
具有指定特征的内存映射文件。
- 属性
例外
              mapName 为 null。
              mapName 是一个空字符串。
              desiredAccessRights 不是有效的 MemoryMappedFileRights 枚举值。
- 或 -
              inheritability 不是有效的 HandleInheritability 枚举值。
请求的访问对于内存映射文件无效。
为 mapName 指定的文件不存在。