Nishant Sivakumar has provided a good answer, but I'd like to point to an alternative suitable for larger files:
] with MemoryMappedViewAccessor
] to read and write data.
Perhaps like this (copied from the documentation):
using (var accessor = mmf.CreateViewAccessor(offset, length))
int colorSize = Marshal.SizeOf(typeof(MyColor));
for (long i = 0; i < length; i += colorSize)
accessor.Read(i, out color);
accessor.Write(i, ref color);
This ability to work with memory mapped files was introduced with .Net 4 - so it's easy to overlook.