I am having troubles reading data in chunks from a file. At second Read I get “A first chance exception of type ‘System.ArgumentException’ occurred in System.IO.dll”.
Am I missing something obvious here?
Dim stream As New MemoryStream(Resources.GetBytes(Resources.BinaryResources.MyFile))
Dim buffer(4095) As Byte
Dim i As Int32
Do While i < stream.Length
i += stream.Read(buffer, i, buffer.Length)
' process data from buffer here
Loop
I have tried your suggestion, but I still get the same error after the first read.
Correct me if i am wrong but:
buffer is zero-based, so [em]buffer(4095)[/em] has 4096 elements; why should I subtract 1 from its length?
you can specify buffer as large as you want, even if my resource is let’s say 4k and I specify buffer as 8k, .net will read 4k and return the number of elements it managed to read, you won’t get any errors; at least that’s how it works in full .net framework.
if your buffer is 8k and your recource is 4k then
i must be 4k after the first step.
if i is still < stream.Length then i must be buffer.length after the first step,
so the buffer.length must be < stream.Length
Btw, my resource is 61440 bytes, so there should be 15 reads.
I have tried “Dim i As Int32 = 0”, but as expected it didn’t change anything.
Maybe you can try to put a bigger file in your project Resources section and try to replicate this issue so that we could be sure it is not happening just for me?