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.File Open(i, Filename, OpenMode.Binary , OpenAccess.Read ,
OpenShare.Defau lt)
Dim iByteArray() As Byte
ReDim iByteArray(LOF( i))
FileSystem.File Get(i, iByteArray, True)
FileSystem.File Close(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 3 12753
"Shaun Merrill" <a@a.com> wrote in
news:OW******** ******@TK2MSFTN GP12.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.File Open(i, Filename, OpenMode.Binary , OpenAccess.Read , OpenShare.Defau lt) Dim iByteArray() As Byte ReDim iByteArray(LOF( i)) FileSystem.File Get(i, iByteArray, True) FileSystem.File Close(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********@rog ers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Shaun,
This should be enough
\\\
Dim fs As New IO.FileStream(F ilePath, _
IO.FileMode.Ope n)
Dim br As New IO.BinaryReader (fs)
abyt = br.ReadBytes(CI nt(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.File Open(i, Filename, OpenMode.Binary , OpenAccess.Read , OpenShare.Defau lt) Dim iByteArray() As Byte ReDim iByteArray(LOF( i)) FileSystem.File Get(i, iByteArray, True) FileSystem.File Close(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
"Cor Ligthert" <no************ @planet.nl> schrieb: Dim fs As New IO.FileStream(F ilePath, _ IO.FileMode.Ope n) Dim br As New IO.BinaryReader (fs) abyt = br.ReadBytes(CI nt(fs.Length))
.... for files with length <= 'Int32.MaxValue '.
;-)
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 StreamObject.Read(myByteArray,
0, myLength)
--
|
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 stopped me in my tracks. I must
have used FileStream.Read a thousand times and it never dawned on me that I
never have to specify either an "out" or "ref" when I pass the byte array
that I pass to this function- yet it returns the bytes read from the...
|
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 data types .
I can re-create the structure in c# with LayoutKind.Explicit, but how do I
convert this structure to a byte array for use with the Socket.Send()
method?
|
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 data file.
If File.Exists(ImgDest) Then
Console.WriteLine("{0} already exists!", ImgDest)
Return
End If
Dim fs As New FileStream(ImgDest, FileMode.CreateNew)
|
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 i want to send the large byte array thro' the
socket connection.Plz help me....with example ...
| |
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 *)a2;
// now a1=a2, a1=a2, etc.
|
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 have tried several methods, but here is the one I thought would have
worked.
Dim strReturn As String
Dim arBytes() As Byte
arBytes = System.IO.File.ReadAllBytes(<myfile>)
|
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}
System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();
string s = new string((char)107, 1);
s += new string((char)62, 1);
|
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
|
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 know I can create a temporary array and call the CopyTo function to copy the data in that array, but I was wondering this this piece of code below with LinQ would do the same.
return (from m in Enumerable.Range(0, Size) select RawContent);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |