Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - MemoryMappedFile , what MemoryMappedFileAccess.ReadExecute do or when should i use it?

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 ?

like image 992
Salo7ty Avatar asked Nov 27 '25 23:11

Salo7ty


1 Answers

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.

like image 169
Athanasios Kataras Avatar answered Nov 30 '25 12:11

Athanasios Kataras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!