Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C# 4.0

Programming Memory-Mapped Files with the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.64/5 (38 votes)
4 Jan 2011CPOL8 min read 170.4K   4.5K   125  
An introduction to MMF and shared memory in .NET applications.
using System;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace NetMMFCopyFile
{
    class Program
    {
        static void Main(string[] args)
        {
            int offset = 0;
            int length = 0;
            byte[] buffer;

            if (args.GetLength(0) != 2)
            {
                Console.WriteLine("Usage: NetMMFCopyFile.exe file1 file2");
                return;
            }

            FileInfo fi = new FileInfo(args[0]);
            length = (int)fi.Length;

            // Create unnamed MMF
            using (var mmf1 = MemoryMappedFile.CreateFromFile(args[0], FileMode.OpenOrCreate, null, offset + length))
            {
                // Create reader to MMF
                using (var reader = mmf1.CreateViewAccessor(offset, length, MemoryMappedFileAccess.Read))
                {
                    // Read from MMF
                    buffer = new byte[length];
                    reader.ReadArray<byte>(0, buffer, 0, length);
                }
            }

            // Create disk file
            using (FileStream fs = File.Create(args[1]))
            {
                fs.Close();
            }
 
            // Create unnamed MMF
            using (var mmf2 = MemoryMappedFile.CreateFromFile(args[1], FileMode.Create, null, offset + length))
            {
                // Create writer to MMF
                using (var writer = mmf2.CreateViewAccessor(offset, length, MemoryMappedFileAccess.Write))
                {
                    // Write to MMF
                    writer.WriteArray<byte>(0, buffer, 0, length);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect GuestLogix Inc.
Canada Canada
Jun is an experienced software architect. He wrote his first computer code on the tape machine for a "super computer". The tape machine reads holes on the black pape tape as source code. When manually fixing code, you need a punch and tranparent tape. To delete code, you block holes or cut off a segment and glue two ends together. To change code, you block old holes and punch new holes. You already know how to add new code, don't you? Anyway, that was his programming story in early 1980's.

Jun completed university with the specialty in oceanography, and graduate study in meteorology. He obtained his Ph.D. in physics. Jun has worked in a number of different areas. Since mid-90's, he has been working as a software professional in both military & commercial industries, including Visual Defence, Atlantis Systems International and Array Systems Computing.

Currently, Jun is an architect at GuestLogix, the global leader in providing onboard retail solutions for airlines and other travel industries. He is also the founder of Intribute Dynamics, a consulting firm specialized in software development. He has a personal blog site, although he is hardly able to keep it up to date.

In his spare time, Jun loves classic music, table tennis, and NBA games. During the summer, he enjoyes camping out to the north and fishing on wild lakes.

Comments and Discussions