473,473 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

This should be simple! Read a File into a Byte Array

Hey, here is my code. I am trying to read a file into a byte array. Please
show me how this can work.
Obviously I need to read into a dynamic array because it could read any
file. So why does VB.NET give me the ability to read into a Byte Array if
it doesn't allow me to use a variable sized array?

Dim i As Short = FreeFile()
FileSystem.FileOpen(i, Filename, OpenMode.Binary, OpenAccess.Read,
OpenShare.Default)
Dim iByteArray() As Byte
ReDim iByteArray(LOF(i))
FileSystem.FileGet(i, iByteArray, True)
FileSystem.FileClose(i)

This code bombs on FileGet(), throwing an EndOfStream exception "Unable to
read beyond the end of the stream."
Even if I use no Redim statement, I get "Cannot determine array type because
it is Nothing."

Obviously, the ArrayIsDynamic parameter is set to True. What am I doing
wrong?!?

~ Frustrated

Nov 21 '05 #1
3 12701
"Shaun Merrill" <a@a.com> wrote in
news:OW**************@TK2MSFTNGP12.phx.gbl:
Hey, here is my code. I am trying to read a file into a byte array.
Please show me how this can work.
Obviously I need to read into a dynamic array because it could read
any file. So why does VB.NET give me the ability to read into a Byte
Array if it doesn't allow me to use a variable sized array?

Dim i As Short = FreeFile()
FileSystem.FileOpen(i, Filename, OpenMode.Binary, OpenAccess.Read,
OpenShare.Default)
Dim iByteArray() As Byte
ReDim iByteArray(LOF(i))
FileSystem.FileGet(i, iByteArray, True)
FileSystem.FileClose(i)

This code bombs on FileGet(), throwing an EndOfStream exception
"Unable to read beyond the end of the stream."
Even if I use no Redim statement, I get "Cannot determine array type
because it is Nothing."

Obviously, the ArrayIsDynamic parameter is set to True. What am I
doing wrong?!?

I think you need to set the preserve keyword:

Redim preserve ibyteArray(LOF(i))

Also, you should be dimming LOF(i) - 1, because arrays are 0-based (so a
byte array of 19999 is really 20000 bytes)

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2
Shaun,

This should be enough

\\\
Dim fs As New IO.FileStream(FilePath, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
///

I hope this helps?

Cor

"Shaun Merrill" <a@a.com>
Hey, here is my code. I am trying to read a file into a byte array.
Please show me how this can work.
Obviously I need to read into a dynamic array because it could read any
file. So why does VB.NET give me the ability to read into a Byte Array if
it doesn't allow me to use a variable sized array?

Dim i As Short = FreeFile()
FileSystem.FileOpen(i, Filename, OpenMode.Binary, OpenAccess.Read,
OpenShare.Default)
Dim iByteArray() As Byte
ReDim iByteArray(LOF(i))
FileSystem.FileGet(i, iByteArray, True)
FileSystem.FileClose(i)

This code bombs on FileGet(), throwing an EndOfStream exception "Unable to
read beyond the end of the stream."
Even if I use no Redim statement, I get "Cannot determine array type
because it is Nothing."

Obviously, the ArrayIsDynamic parameter is set to True. What am I doing
wrong?!?

~ Frustrated

Nov 21 '05 #3
"Cor Ligthert" <no************@planet.nl> schrieb:
Dim fs As New IO.FileStream(FilePath, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))


.... for files with length <= 'Int32.MaxValue'.

;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hardy Wang | last post by:
Hi all: The Stream object from WebRequest.GetResponseStream() is non-seekable. How can I convert this stream to a byte array? For ordinary Stream (seekable), I can use...
3
by: Gene | last post by:
I'm not sure I understand this behavior (at least in the context of C#). I understand the use of "ref" and "out" keywords as they apply to method arguments, and then something this morning...
2
by: Shreddy | last post by:
Hi, I'm trying (or struggling) to convert some C code to C#. The existing C client is sending a structure via a TCP socket to a network server. The structure contains a mix of int and char...
3
by: JamesB | last post by:
I am saving image files from my website using the downloaddata on the web client. This gives me a byte array, which I am then saving to a file with the following code: ' Create the new, empty...
1
by: sridev | last post by:
hi all, i have a byte array which has length of 10,000.I have to break this byte array into smaller parts of length 512 and save this values into another byte array. I am doing this because...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
6
by: AG | last post by:
I have a file that contains ASCII and Extended ASCII characters. I need to get the file contents into a string, but the Extended ASCII characters (dec 128 and 129) are being changed to dec 63. I...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
18
by: wquatan | last post by:
Does someone know an (fast) example of reading files in VBA, Byte per Byte ? I need to be able to take action on every single byte Thx
2
Airslash
by: Airslash | last post by:
Hello, I'm currently working with byte arrays to hold data transmitted over the network, and i'm looking for a way to copy a section of that byte array and return it from a function result. I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.