Creating a byte array using long data type?
Most of the time, when we read a file stream into a byte array, we will write the following code: -
Dim inputStream As New System.IO.FileStream(filePath, IO.FileMode.Open)
Dim fileLength As Integer= CType(inputStream.Length, Integer)
Dim input(fileLength) As Byte
Using inputStream
inputStream.Read(input, 0, fileLength)
End Using
But here we have to convert Length to an integer type (line 2 of the above code), since we cannot declare a byte array using a long data type (with the strict on option). Is this good practice? What is the job for this problem?
0
source to share
1 answer
It is good practice to use File.ReadAllBytes
instead of everything:
Dim input = File.ReadAllBytes(filePath)
By the way, if your file is that big (more than 4 GB), you won't want to load it all at once into a byte array, as it will take (and in a 32-bit managed process you won't be able to get it at all, even if you have more RAM).
+2
source to share