Memory Handling The mapped file in C # directly from memory

Is it possible to open a file with a memory map in C # just like opening a file directly in windows, say, for example, I create a file with a memory map. through the following code.

using System; using System.IO.MemoryMappedFiles; namespace ConsoleApplication1 { class Program { static void Main() { MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test.txt", 5); MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(); var arun = new[] {(byte)'a', (byte)'r', (byte)'u', (byte)'n'}; for (int i = 0; i < arun.Length; i++) accessor.Write(i, arun[i]); Console.WriteLine("Memory-mapped file created!"); Console.ReadLine(); // pause till enter key is pressed accessor.Dispose(); mmf.Dispose(); } } } 

I need to open the file directly. Is it possible to open the file through

Process.start("test.txt");

from another process instead of reading values ​​with code.

  MemoryMappedFile mmf1 = MemoryMappedFile.OpenExisting("test.txt"); MemoryMappedViewAccessor accessor1 = mmf1.CreateViewAccessor(); var value = accessor1.ReadByte(4); 

If it is possible to open a memory mapping file directly? Please let me know.

+6
source share
2 answers

Starting with the .NET Framework version 4, you can use managed code to access memory-mapped files in the same way as the native Windows memory-mapped file access functions, as described in the "Managing" section of Win32 memory files in MSDN library.

+4
source

The MemoryMappedFile.CreateNew method creates a file mapping object that is not associated with a file on disk. So no, you cannot open the file directly because it does not exist. (Such file mapping objects are supported by the system page file as described here .)

Instead, you can use the MemoryMappedFile.CreateFromFile method if you want to associate the file mapping object with the actual file.

+2
source

Source: https://habr.com/ru/post/906636/


All Articles