6

Is it possible to open memory mapped file in C# directly just like opening files directly in windows, say for example, I'm creating memory mapped file. through 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 like opening file through

Process.start("test.txt");

from another process instead of reading values by the code .

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

If it possible to open memory mapped file directly? Please let me know.

5
  • Why are you using a memory mapped file just to do simple file operations on it? Commented Jan 24, 2012 at 13:42
  • though it simple file operation but difficult to access directly from memory isn't it? Commented Jan 24, 2012 at 14:46
  • Nowhere in your code you are accessing the content like your are reading memory. Commented Jan 24, 2012 at 16:07
  • @leppie: that's sample code ("say for example") intended to demonstrate what he wants to achieve, he presumably has a sensible reason to be using file mapping in his real code. Commented Jan 24, 2012 at 20:51
  • still i'm expecting to be actual file,yours not helpful Commented Jan 18, 2013 at 12:28

2 Answers 2

4

Starting with the .NET Framework version 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files in Win32 in the MSDN Library.

Sign up to request clarification or add additional context in comments.

1 Comment

all i get about read/write of memeory mapped file but i didn't get open memeory mapped file as a process
2

The MemoryMappedFile.CreateNew method creates a file mapping object that is not associated with a file on disk. So no, you can't open the file directly, because there isn't one. (Such file mapping objects are backed by the system paging file, as described here.)

You can use the MemoryMappedFile.CreateFromFile method instead if you want to associate the file mapping object with an actual file.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.