-1

So I have been searching for a way to Read the bytes of a file into a array, Ive tried using File.ReadAllBytes() and FileStreams but both of those seem to not be able to support files more than 2 gb. Is there any at to get a bytes array of a file that is more then 2 gb IN C#!

Thanks

xCrypt0
  • 35
  • 1
  • 4

2 Answers2

3

No object can be larger than 2 GB

You have two options:
1. Use a Stream to read and process the file.
2. Use a MemoryMappedFile if you are using .NET 4.0

Nothing good can come of loading large files into the memory. It will cost you the time of reading the file into the memory (IO operation) and the time of processing it, not mention the high possibility of getting OutOfMemory Exception.

Unless you absolutely have to, you can read every 1 GB or so of data into an object using a stream until you are finished reading the file into memory.

Vanna
  • 746
  • 1
  • 7
  • 16
2

By default, there is a 2 GB limit on the size of any single object.

By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit. For more information, see the element. 64-bit Applications

IF you enable gcAllowVeryLargeObjects you can go over 2 GB but still have the following limitation:

The maximum number of elements in an array is System.UInt32.MaxValue. <gcAllowVeryLargeObjects> Element

However gcAllowVeryLargeObjects will not solve the issue since you are using a byte array you are limited to 2 GB due to the number of elements.

Currently the only way to process a file larger than 2 GB, using a byte array, is to use some sort of buffered read where you process a portion of the file at a time until the entire file has been processed.