locked
Efficient way of reading binary file into byte array? RRS feed

  • Question

  • What is the most efficient (fastest) way to read a binary file into a byte array?
    Thursday, February 11, 2010 2:29 PM

Answers

All replies

  • File.ReadAllBytes?
    • Proposed as answer by Louis.fr Thursday, February 11, 2010 3:51 PM
    Thursday, February 11, 2010 2:34 PM
  • Look at the FileStream.Read method.

    -Scosby
    • Marked as answer by BBauer42 Thursday, February 11, 2010 4:15 PM
    Thursday, February 11, 2010 3:10 PM
  • Since you want it in a byte array File.ReadAllbytes as proposed by Tergiver would be better then FileStream.Read since that would require you to write/maintain some extra code to open/close the file.
    Thursday, February 11, 2010 4:23 PM
  • Memory mapping a file is the most I/O efficient way. .NET 4.0 will
    include support for this. There is also a wrapper for the Win32 Memory
    Mapped File services which can be used with other versions of .NET,
    http://github.com/tomasr/filemap/

    Keep in mind memory mapping isn't always the best solution. See
    http://en.wikipedia.org/wiki/Memory-mapped_file#Drawbacks
    Thursday, February 11, 2010 5:25 PM