From: Martin H. on
Hello Tom,

if it is just the file size you want,
this can be done much easier and quicker by using
the FileInfo object.

fileSize = New System.IO.FileInfo(path).Length

Best regards,

Martin
From: Davej on
On May 13, 2:19 pm, Tom Shelton <tom_shel...(a)comcast.invalid> wrote:
>
> You still did not provide information about the file...  What kind of
> content is it?  Further - what line does the exception occur on?  
> Also, you said your getting an ArgumentException - that is documented
> to be thrown when the bytes or char represents a unicode surrogate
> pair.
>

Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.
From: Armin Zingler on
Am 14.05.2010 15:33, schrieb Davej:
> On May 13, 2:19 pm, Tom Shelton <tom_shel...(a)comcast.invalid> wrote:
>>
>> You still did not provide information about the file... What kind of
>> content is it? Further - what line does the exception occur on?
>> Also, you said your getting an ArgumentException - that is documented
>> to be thrown when the bytes or char represents a unicode surrogate
>> pair.
>>
>
> Oh, I want to be able to read any file. I am old, from the ancient
> times when a binary reader reading byte by byte could read any file.
> Thanks.

In general, you can use the BinaryReader.
But you have a specific problem. Therefore more specific information is helpful,
like the line of the exception. Without knowing the type of file,
we can not know if ReadChar can work well. It depends on the file format
and the character encoding used. A char is not a byte. If you want to read
byte by byte, use the ReadByte method instead. Or the Read(byte(), ...)
method to read into an array of bytes.


--
Armin
From: Mayayana on


--
--
"Davej" <galt_57(a)hotmail.com> wrote in message
news:50bb2763-325a-40db-aca2-d531f91e2244(a)e1g2000yqe.googlegroups.com...
On May 13, 2:19 pm, Tom Shelton <tom_shel...(a)comcast.invalid> wrote:
>
> You still did not provide information about the file... What kind of
> content is it? Further - what line does the exception occur on?
> Also, you said your getting an ArgumentException - that is documented
> to be thrown when the bytes or char represents a unicode surrogate
> pair.
>

Oh, I want to be able to read any file. I am old, from the ancient
times when a binary reader reading byte by byte could read any file.
Thanks.
>

:)

Maybe this helps?
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes.aspx

It refers to a ReadByte method. Your code seems to be
reading characters. Oddly, though, the link above also
mentions the ArgumentException error in connection with
surrogate pairs, even in the context of bytes. It mentions
a "unicode decoder". I don't know whether that's a misprint
or whether .Net truly can't just read bytes without any funny
business.

I'm no .Net expert, but you seem to be getting answers
to everything but the question you asked, so I too pity.


From: Patrice on
Hi,

> Oh, I want to be able to read any file. I am old, from the ancient
> times when a binary reader reading byte by byte could read any file.
> Thanks.

Still you don't tell where it happens as asked by Tom.

My guess is that it happens at PeekChar. If you check
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.peekchar.aspx
you'll see that ArgumentException happens when the char is not valid with
the current encoding. The trick is that BinaryReader is not "just" a binary
reader. It exposes an underlying stream as a binary stream but still use
some encoding( UTF8 by default).

If all you need is to read bytes does it work if you just use FileStream ?
Else you could pass the encoding used by your file as a parameter when
building the BinaryReader (Text.Encoding.Default ?).

The smallest code sample that repro the problem could also help (here likely
with a first section that writes the file). For example :
Sub Main()
Const Path As String = "Test.txt"
' Write
Dim fs As New FileStream(Path, FileMode.Create)
Dim bw As New BinaryWriter(fs)
For i As Integer = 0 To 1000
bw.Write(i)
Next
bw.Close()
fs.Dispose()
' Read
fs = New FileStream(Path, FileMode.Open)
Dim br As New BinaryReader(fs)
Do While br.PeekChar <> -1 ' <====== FAILS HERE
Debug.WriteLine(br.ReadChar)
Loop
br.Close()
fs.Dispose()
End Sub

If you use Dim br as New BinaryReader(fs,Text.Encoding.Default) it works. In
the first version, a value is written that is not valid UT8 character so it
fails later when using PeekChar. With the Text.Encoding.Default (which is
ANSI), all character values are valid and the problem goes away.

--
Patrice