Encoding problems

  • Thread starter Thread starter WH
  • Start date Start date
W

WH

Hi,
I use a Streamreader in VB2005 to read text files I made before in NotePad,
but it can't read characters like "é" or "ç". They are omitted, or replaced
by other characters. I tried different encodings, but I can't fix the
problem. In VB6 everything works well. I live in Belgium (Duch language).
Can anyone help me?
Thanks,
WH
 
I would use UTF8 encoding.

This example outputs readable character only in UTF8.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim sw As System.IO.StreamWriter = New
System.IO.StreamWriter("c:\temp\test.txt", False,
System.Text.Encoding.ASCII)
sw.WriteLine("-------------------")
sw.WriteLine("ASCII")
sw.WriteLine("Ligne 2: éàèêô.")
sw.WriteLine("-------------------")
sw.Close()

sw = New System.IO.StreamWriter("c:\temp\test.txt", True,
System.Text.Encoding.UTF8)
sw.WriteLine("-------------------")
sw.WriteLine("UTF8")
sw.WriteLine("Ligne 2: éàèêô.")
sw.WriteLine("-------------------")
sw.Close()

End Sub


--


HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
S2i web inc. (www.s2i.com)
http://www.emoreau.com/
 
WH,

Are you sure that they are gone, I had a while as well that problem, but I
had just set my advance language setting (3th tab in country setting) to
one one which had not these characthers, and therefore it was not showed in
notepad (although I thought it was in some others)..

The streamreader cannot see if it is such a kind of characters or not.

I hope this helps,

Cor
 
WH said:
I use a Streamreader in VB2005 to read text files I made before in
NotePad, but it can't read characters like "é" or "ç". They are omitted,
or replaced by other characters. I tried different encodings, but I can't
fix the problem. In VB6 everything works well. I live in Belgium (Duch
language).

Notepad stores the file with the system's Windows ANSI code page by default.
Thus you have this encoding to read the file. VB6's file access functions
use the Windows ANSI encoding too. That's why reading the file in VB6
works. In .NET you have to pass the appropriate encoding to the
'StreamReader' object's constructor. To obtain the system's Windows ANSI
code page use 'Encoding.Default':

\\\
Imports System.IO
Imports System.Text
....
Dim Reader As New StreamReader(..., Encoding.Default)
....
///
 
Back
Top