By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Read Binary File

P: n/a
Hi All

I need to read a file to an array of bytes (any type of file)
I tried using FileStream and BinaryReader but it doesn't seem to work

Thank you,
Shmuel
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Shmuel,
You don't really need the BinaryReader if you simply want to read the entire
file into a byte array.

Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))

' with a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim reader As New BinaryReader(input)
Dim bytes() As Byte
bytes = reader.ReadBytes(CInt(input.Length))

Hope this helps
Jay
"S Shulman" <sm*******@hotmail.com> wrote in message
news:OY**************@TK2MSFTNGP11.phx.gbl...
Hi All

I need to read a file to an array of bytes (any type of file)
I tried using FileStream and BinaryReader but it doesn't seem to work

Thank you,
Shmuel

Nov 21 '05 #2

P: n/a
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))


Attention!

<URL:http://www.yoda.arachsys.com/csharp/readbinary.html>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
Herfried,
Attention!
Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
M S Herfried K. Wagner
BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de... Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))


Attention!

<URL:http://www.yoda.arachsys.com/csharp/readbinary.html>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

P: n/a
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Attention!


Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...


ACK.
M S Herfried K. Wagner


BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...


;-) -- it stands for "Microsoft". "MSFT" would not fit into the signature.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
Just learning VB.Net and was interested in the blocking you mentioned. If I
understand correctly, the following is not guaranteed to read all the bytes:

dim fs as FileStream(myfilename, filemode.Open)
dim byt() as byte
dim r as binaryreader(fs)
byt = r.ReadBytes(fs.Length)

Do I need to use blocking to insure I get all the file into byt? I didn't
see this mentioned in the VB.Net Documentation on the binary reader class.

"Jay B. Harlow [MVP - Outlook]" wrote:
Herfried,
Attention!


Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't
consider it important enough in my initial answer...
M S Herfried K. Wagner


BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...

Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Either of the following works.

' without a BinaryReader
Dim input As New FileStream("myfile.bin", FileMode.Open)
Dim bytes(CInt(input.Length - 1)) As Byte
input.Read(bytes, 0, CInt(input.Length))


Attention!

<URL:http://www.yoda.arachsys.com/csharp/readbinary.html>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #6

P: n/a
Dennis,
Reading Jon's site (the link Herfried gave) you should block (read in
chunks) the read, when using Stream.Read, at the very least you should check
the return value from Stream.Read to ensure that you read what you expected
to read!
BinaryReader.ReadBytes does not allow you to block the read, I would expect
internally it uses code similar to Jon's code.
Hope this helps
Jay
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
Just learning VB.Net and was interested in the blocking you mentioned. If I understand correctly, the following is not guaranteed to read all the bytes:
dim fs as FileStream(myfilename, filemode.Open)
dim byt() as byte
dim r as binaryreader(fs)
byt = r.ReadBytes(fs.Length)

Do I need to use blocking to insure I get all the file into byt? I didn't
see this mentioned in the VB.Net Documentation on the binary reader class.

"Jay B. Harlow [MVP - Outlook]" wrote:
Herfried,
Attention!


Careful! ;-)

I was thinking of mentioning you really should "block" the reads, but didn't consider it important enough in my initial answer...
M S Herfried K. Wagner


BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List, and MS refers to Mobile Station on there, not Microsoft...

Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2o************@uni-berlin.de...
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
> Either of the following works.
>
> ' without a BinaryReader
> Dim input As New FileStream("myfile.bin", FileMode.Open)
> Dim bytes(CInt(input.Length - 1)) As Byte
> input.Read(bytes, 0, CInt(input.Length))

Attention!

<URL:http://www.yoda.arachsys.com/csharp/readbinary.html>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #7

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
M S Herfried K. Wagner


BTW: What is the "M S" in your signature? I'm on the Marklin Mailing List,
and MS refers to Mobile Station on there, not Microsoft...


Look at the left-hand side of all three lines of his .sig:
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
In case you're still not getting it, try this:
M S
M V P
V B

Nov 21 '05 #8

P: n/a
Jeff,

You are right this is definitly something what is Herfried,

http://www.mmv.co.jp/products/

:-)

Cor
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.