【问题标题】:Is It Possible for Two Applications to Create a Memory Mapped File with the Same Name?两个应用程序可以创建同名的内存映射文件吗?
【发布时间】:2020-08-27 18:12:14
【问题描述】:

我在 MSDN 上阅读

内存映射文件包含虚拟内存中文件的内容。文件和内存空间之间的这种映射使应用程序(包括多个进程)能够通过直接读取和写入内存来修改文件。

来源: https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files

在另一个论坛上:

内存映射文件可以在多个进程之间共享。进程可以使用创建文件的进程分配的通用名称映射到同一个内存映射文件。

如果我使用来自两个不同应用程序的MemoryMappedFile.CreateFromFile 创建两个内存映射文件,并为它们分配相同的名称,会发生什么情况?这会抛出异常吗?

【问题讨论】:

    标签: c# memory-mapped-files


    【解决方案1】:

    经过测试发现抛出了异常:

    Unhandled exception. System.IO.IOException: Cannot create a file when that file already exists.
       at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateCore(FileStream fileStream, String mapName, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, Int64 capacity)
       at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(String path, FileMode mode, String mapName, Int64 capacity, MemoryMappedFileAccess access)
       at LiveFileReader.AsyncLiveFileReader..ctor(String filePath) in ...
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2012-06-06
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多