to read a textfile regarding his format

A

andreas

A textfile can have different formats like ANSI,UTF8, Unicode ...
Using streamreader for a textfile can have different results for chars like
é,à,è....depending of the fileformat.
To solve this problem I have in the code analysed the chars in the beginning
of the textfile, named the BOM to see the system.text.encoding for using
this information in the streamreader.
But my question is :
Is there a way that vb.net can do this automatically?
Thanks for any response
 
M

Michel Posseth [MCP]

with the boolean in the constructor of the streamreader you can set it to
automaticly detect the Byte Order Mark

regards

Michel Posseth
 
A

andreas

I have placed the code
dim sr as new streamreader(filename,true)
but the sr didn't recognise a ANSI file made with notepad
the chars é,à,è.... are disapeared
I missed something?
 
A

arthurjr07

try this, maybe this will help.

Dim myStreamWriter As New StreamWriter(FileName, False,
System.Text.Encoding.Default)
 
A

andreas

I don't think that it helps because a system.text.encoding.default gives a
ANSI encoding and mentioning nothing gives UTF8
 
A

arthurjr07

I copied and paste this char(é,à,è) in the notepad and
save it as UTF8 encoding.
then i tried the code below and i have no problem.
dim sr as new streamreader(filename,true)
and
Dim myStreamReader As New System.IO.StreamReader(objFile.FullName,
System.Text.Encoding.UTF8, False)

please check your textfile.
 
H

Herfried K. Wagner [MVP]

I copied and paste this char(é,à,è) in the notepad and
save it as UTF8 encoding.
then i tried the code below and i have no problem.
dim sr as new streamreader(filename,true)
and
Dim myStreamReader As New System.IO.StreamReader(objFile.FullName,
System.Text.Encoding.UTF8, False)


Both will work if the file is stored using UTF-8. By specifying no encoding
in the constructor, UTF-8 is used.
 
A

andreas

Thanks everyone but this is not what I want.
I know what is written.
I want to read the textfile without knowing what the format is
(ANSI,UTF8,Unicode...)
Now, I can do that only by code by looking at the first bytes in the file.
My question is , is there a method that the code read automaticly the right
format?
 
H

Herfried K. Wagner [MVP]

andreas said:
Thanks everyone but this is not what I want.
I know what is written.
I want to read the textfile without knowing what the format is
(ANSI,UTF8,Unicode...)
Now, I can do that only by code by looking at the first bytes in the file.
My question is , is there a method that the code read automaticly the
right
format?

AFAIK no.
 

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