Ahmed M. Taher
Ahmed M. Taher

Reputation: 712

Read file bytes using MemoryMappedFile

How can I read file bytes using MemoryMappedFile and put it into byte[] array?

Upvotes: 3

Views: 1572

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

Assuming that you only want to read a portion of the file, something like this should work:

long offset = 0x10000000; // 256 megabytes 
long length = 100; 
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(@"c:\Test.data"))
    {
        using (MemoryMappedViewStream stream = CreateViewStream(offset, length))
        {
            byte[length] bytes;
            int bytesRead = stream.Read(bytes, 0, (int)length);
        }
    }

If you want to read the entire file, you really shouldn't be using a MemoryMappedFile object in the first place.

Upvotes: 6

Related Questions