FileStream

T

Tony Johansson

Hello!

As far as I understand if I'm about to read all bytes from a file
I must then first find out how many bytes this file consist of and
then use the Read method on the FileStream object.

So my question is if it's possible to read all bytes from
a file without finding how many bytes this file consist of.

//Tony
 
J

Jeroen Mostert

Tony said:
As far as I understand if I'm about to read all bytes from a file
I must then first find out how many bytes this file consist of and
then use the Read method on the FileStream object.
Not at all, where did you get that idea?
So my question is if it's possible to read all bytes from
a file without finding how many bytes this file consist of.
Sure:

byte[] b = File.ReadAllBytes("test.txt");
 
A

Arne Vajhøj

Tony said:
As far as I understand if I'm about to read all bytes from a file
I must then first find out how many bytes this file consist of and
then use the Read method on the FileStream object.

So my question is if it's possible to read all bytes from
a file without finding how many bytes this file consist of.

You read to EOF.

EOF can be detected in various ways:
- Stream read byte array returns 0 bytes read
- Stream read single byte returns <0
- StreamReader read line returns null
etc.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top