UnauthorizedAccessException on MemoryMappedFile in C# 4

后端 未结 4 1872
醉梦人生
醉梦人生 2021-01-11 12:20

I wanted to play around with using a MemoryMappedFile to access an existing binary file. If this even at all possible or am I a crazy person?

The idea would be to ma

相关标签:
4条回答
  • 2021-01-11 12:31

    If the size is more than the file length, it gives the UnAuthorized Access exception. Because we are trying to access memory beyond the limits of the file.

    var view = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read);
    
    0 讨论(0)
  • 2021-01-11 12:33

    I know this is an old question, but I just ran into the same error and was able to solve it.

    Even though I was opening the MemoryMappedFile as read-only (MemoryMappedFileRights.Read) as you are, I also needed to create the view accessor as read-only as well:

    var view = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read);
    

    Then it worked. Hope this helps someone else.

    0 讨论(0)
  • 2021-01-11 12:45

    It is difficult to say what might be going wrong. Since there is no documentation on the MSDN website yet, your best bet is to install FILEMON from SysInternals, and see why that is happening.

    Alternately, you can attach a native debugger (like WinDBG) to the process, and put a breakpoint on MapViewOfFile and other overloads. And then see why that call is failing.

    0 讨论(0)
  • 2021-01-11 12:54

    Using the .CreateViewStream() from the instance of MemoryMappedFile removed the error from my code. I was unable to get .CreateViewAcccessor() working w/the access denied error

    0 讨论(0)
提交回复
热议问题