3

I learned some basics about MemoryMappedFile and I saw that there is an enum member called MemoryMappedFileAccess.ReadExcute.

I thought it means when you open an exe file, it (my programme) execute it (.exe file) and read the bytes that inside it (.exe file) but when i execute the program it throw me an error:

Acceess to the path is denied [UnauthorizedAccessException]

mycode:

 static void Main(string[] args)
    {
        FileStream fs = new FileStream("programe.exe", FileMode.OpenOrCreate, FileAccess.ReadWrite, 
FileShare.ReadWrite);


        MemoryMappedFile memory = MemoryMappedFile.CreateFromFile(fs, "mapname", 0, 
MemoryMappedFileAccess.ReadExecute,null,0,false);



        MemoryMappedViewAccessor mmr = memory.CreateViewAccessor(0, fs.Length, MemoryMappedFileAccess.Read);






        Console.ReadKey();
    }

Can any one explain it to me ?

2
  • @jdweng okay , what can i do to solve the exception
    – Salo7ty
    Dec 5, 2019 at 7:13
  • @CaiusJard yes i have read it , okay what is the difference between Read and ReadExcecute ??
    – Salo7ty
    Dec 5, 2019 at 7:14

1 Answer 1

2

Plenty of things to try here:

  1. Try opening with Read to see if this fixes your problem
  2. Try adding the full file path (in case it's not opening)
  3. Add the length of the file when you open it

Check three and one below:

static void Main(string[] args)
{
    // Try with Read here and Read on your create view to see if anything changes
    FileStream fs = new FileStream("programe.exe", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
    // Set the length of the file here.
    MemoryMappedFile memory = MemoryMappedFile.CreateFromFile(fs, "mapname", fs.Length, MemoryMappedFileAccess.ReadExecute,null,0,false);

    MemoryMappedViewAccessor mmr = memory.CreateViewAccessor(0, fs.Length, MemoryMappedFileAccess.Read);

    Console.ReadKey();
}
  1. Then check that your file "programe.exe", has the appropriate rights in your filesystem

Based on the documentation the ReadExecute is just a Read access right for executable files:

ReadExecute 4

Read access to the file that can store and run executable code.

4
  • 1
    thanks for reply , but i have change my code with your code and still throw the exception and when i replace ReadExecute with Read , it works fine , but i want to know what is the difference between Read, ReadExecute ??
    – Salo7ty
    Dec 5, 2019 at 7:20
  • 1
    Whilst a fine answer, I think OP wants an explanation too. - "what MemoryMappedFileAccess.ReadExecute do or when should i use it? Can any one explain it to me ?"
    – user585968
    Dec 5, 2019 at 7:33
  • Not sure. You may have to set HandleInheritability. Read learn.microsoft.com/en-us/dotnet/api/…
    – jdweng
    Dec 5, 2019 at 10:50
  • Truth be told the ReadExecute does not make sense for Mapped Files. Mapped files are used for sharing data in memory between processes and I have yet to find a good example of ReadExecute usage. Dec 5, 2019 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.