Memory-Mapped Files : Memory Stream « File Stream « C# / C Sharp






Memory-Mapped Files

  

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        long offset = 0x10000000; // 256 megabytes
        long length = 0x20000000; // 512 megabytes

        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\test.data", FileMode.Open, "ImgA"))
        {
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;
                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    accessor.Write(i, ref color);
                }
            }
        }

    }
}
public struct MyColor
{
    public short Red;
}

   
    
  








Related examples in the same category

1.Create a MemoryStream
2.MemoryStream.Write
3.Use MemoryStream and BinaryWriter to convert decimal to byte arrayUse MemoryStream and BinaryWriter to convert decimal to byte array
4.Use MemoryStream and BinaryReader to convert Byte array to decimalUse MemoryStream and BinaryReader to convert Byte array to decimal
5.MemoryStream.ToArray, MemoryStream.ReadDecimal
6.Deep Copy with MemoryStream
7.Deep clone with MemoryStream
8.Using MemoryStream to Serialize and Desirialize
9.Convert a string into an array of bytes