Nishant Sivakumar has provided a good answer, but I'd like to point to an alternative suitable for larger files:
Use
MemoryMappedFile[
^] 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));
MyColor color;
for (long i = 0; i < length; i += colorSize)
{
accessor.Read(i, out color);
color.Brighten(10);
accessor.Write(i, ref color);
}
}
This ability to work with memory mapped files was introduced with .Net 4 - so it's easy to overlook.
Regards
Espen Harlinn