StreamReader problem with Scandinavian letters (äÄöÖ)

M

Mika M

Hello!

I'm reading text file line by line using the StreamReader like code below
shows. Reading is working fine, but if readed line contains Scandinavian
letters like äÄöÖ then those letters are simply cut off ! Why ??? For
example my lastname Mähönen will be Mhnen. How should I change code to get
all letters as is of the file?

Dim strLine as String
Dim sr As StreamReader = New StreamReader(strFilePath)
Do While ...
strLine = sr.ReadLine()
'...
Loop
sr.Close()

Thank you in advance!

Mika from Finland
 
A

Armin Zingler

Mika M said:
Hello!

I'm reading text file line by line using the StreamReader like code
below shows. Reading is working fine, but if readed line contains
Scandinavian letters like äÄöÖ then those letters are simply cut off
! Why ??? For example my lastname Mähönen will be Mhnen. How should I
change code to get all letters as is of the file?

Dim strLine as String
Dim sr As StreamReader = New StreamReader(strFilePath)
Do While ...
strLine = sr.ReadLine()
'...
Loop
sr.Close()

Which encoding has been used to write the file? I guess it's ANSI. You can
pass the Encoding to the Streamreader:

Dim sr As StreamReader = New StreamReader( _
strFilePath, System.Text.Encoding.Default _
)

If you need a different encoding, have also a look at the other members of
the Encoding class. You can also create a new one using it's GetEncoding
function.
 
H

Herfried K. Wagner [MVP]

* "Mika M said:
I'm reading text file line by line using the StreamReader like code below
shows. Reading is working fine, but if readed line contains Scandinavian
letters like äÄöÖ then those letters are simply cut off ! Why ??? For
example my lastname Mähönen will be Mhnen. How should I change code to get
all letters as is of the file?

Dim strLine as String
Dim sr As StreamReader = New StreamReader(strFilePath)

Replace the line above with
'... = New StreamReader(strFilePath, Encoding.Default)'.

Don't forget to import the namespace 'System.Text'.
 
J

Jay B. Harlow [MVP - Outlook]

Mika,
Dim sr As StreamReader = New StreamReader(strFilePath)

You need to give the proper System.Text.Encoding object to the StreamReader
constructor, for the encoding that the file is in.

The default encoding object is Encoding.UTF8, I suspect you want
Encoding.Default, which is the encoding for your system's current ANSI code
page.

Something like:

Imports System.Text
Dim sr As StreamReader = New StreamReader(strFilePath, Encoding.Default)

Hope this helps
Jay
 

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