StreamReader problem with Scandinavian letters (äÄöÖ)

  • Thread starter Thread starter Mika M
  • Start date Start date
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
 
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.
 
* "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'.
 
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
 
Back
Top