Прочетете на английски Редактиране

Споделяне чрез


MemoryMappedFile.OpenExisting Method

Definition

Opens an existing named memory-mapped file in system memory.

Overloads

OpenExisting(String)

Opens an existing memory-mapped file that has the specified name in system memory.

OpenExisting(String, MemoryMappedFileRights)

Opens an existing memory-mapped file that has the specified name and access rights in system memory.

OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

Opens an existing memory-mapped file that has the specified name, access rights, and inheritability in system memory.

OpenExisting(String)

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

Opens an existing memory-mapped file that has the specified name in system memory.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName);

Parameters

mapName
String

The name of the memory-mapped file.

Returns

A memory-mapped file that has the specified name.

Attributes

Exceptions

mapName is null.

mapName is an empty string.

The file specified for mapName does not exist.

Examples

Opening a Persisted Memory-Mapped File

The following example opens a memory-mapped file named ImgA that has already been created from a file on disk (as shown in the example for the CreateFromFile(String) method).

C#
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);
    }
}

Opening a Non-Persisted Memory-Mapped File

The following example opens a memory-mapped file used for inter-process communication. This code example is part of a larger example provided for the CreateNew(String, Int64) method.

Remarks

The memory-mapped file can be either a persisted memory-mapped file (associated with a file on disk) or non-persisted.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

OpenExisting(String, MemoryMappedFileRights)

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

Opens an existing memory-mapped file that has the specified name and access rights in system memory.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights);

Parameters

mapName
String

The name of the memory-mapped file to open.

desiredAccessRights
MemoryMappedFileRights

One of the enumeration values that specifies the access rights to apply to the memory-mapped file.

Returns

A memory-mapped file that has the specified characteristics.

Attributes

Exceptions

mapName is null.

mapName is an empty string.

desiredAccessRights is not a valid MemoryMappedFileRights enumeration value.

The file specified for mapName does not exist.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

OpenExisting(String, MemoryMappedFileRights, HandleInheritability)

Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs
Source:
MemoryMappedFile.cs

Opens an existing memory-mapped file that has the specified name, access rights, and inheritability in system memory.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);
C#
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);
C#
[System.Security.SecurityCritical]
public static System.IO.MemoryMappedFiles.MemoryMappedFile OpenExisting(string mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights desiredAccessRights, System.IO.HandleInheritability inheritability);

Parameters

mapName
String

The name of the memory-mapped file to open.

desiredAccessRights
MemoryMappedFileRights

One of the enumeration values that specifies the access rights to apply to the memory-mapped file.

inheritability
HandleInheritability

One of the enumeration values that specifies whether a handle to the memory-mapped file can be inherited by a child process. The default is None.

Returns

A memory-mapped file that has the specified characteristics.

Attributes

Exceptions

mapName is null.

mapName is an empty string.

desiredAccessRights is not a valid MemoryMappedFileRights enumeration value.

-or-

inheritability is not a valid HandleInheritability enumeration value.

The requested access is invalid for the memory-mapped file.

The file specified for mapName does not exist.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1