Problem with code page in StreamWriter

F

Frank M.

I am exporting data to a textfile, but I cannot get
special Scandinavian chars to be written correctly. I use
a System.IO.StreamWriter object to write. I use the
System.Text.UTF8Encoding.

Just before the StreamWriter.WriteLine(string) I use the
debugger to see the string which appears as "Præst". But
when I look at the resulting file afterwards, it has been
written as "Præst". I have also tried UTF7Encoding,
ASCIIEncoding and UnicodeEncoding, but to no avail.
Either just the special chars are are written incorrectly
or all chars are written incorrectly.

I just want to write with a standard Windows codepage.
Does anyone have a solution for this?


Regards,

Frank
 
H

Herfried K. Wagner [MVP]

* "Frank M. said:
I am exporting data to a textfile, but I cannot get
special Scandinavian chars to be written correctly. I use
a System.IO.StreamWriter object to write. I use the
System.Text.UTF8Encoding.

Just before the StreamWriter.WriteLine(string) I use the
debugger to see the string which appears as "Præst". But
when I look at the resulting file afterwards, it has been
written as "Præst". I have also tried UTF7Encoding,
ASCIIEncoding and UnicodeEncoding, but to no avail.
Either just the special chars are are written incorrectly
or all chars are written incorrectly.

I just want to write with a standard Windows codepage.
Does anyone have a solution for this?

Use 'Encoding.Default'. This will use the system's codepage.
 
F

Frank M.

Got it!

In the System.Text.Encoding Class there is a shared
method GetEncoding to which you can supply either an
integer or string for identifying the code page. It
returns an Encoding object for that code page. "ISO88591-
1" gives the windows codepage, so:

Dim srTest As System.IO.StreamWriter
Dim sNewFile As String = "<your filename>"

srTest = New IO.StreamWriter(sNewFile, False, _
System.Text.Encoding.GetEncoding("ISO8859-1"))

does the job.


Regards,

Frank M.
 
J

Jay B. Harlow [MVP - Outlook]

Frank,
There is no real need to open a new thread, as that just confuses the issue.
I just want to write with a standard Windows codepage.
Does anyone have a solution for this?

If you want to create a file with the system's current ANSI code page, then
you need to use System.Text.Encoding.Default when you open the StreamWriter,
something like:

Imports System.Text

Dim writer As New StreamWriter("My output File.txt", False,
Encoding.Default)


Hope this helps
Jay

I am exporting data to a textfile, but I cannot get
special Scandinavian chars to be written correctly. I use
a System.IO.StreamWriter object to write. I use the
System.Text.UTF8Encoding.

Just before the StreamWriter.WriteLine(string) I use the
debugger to see the string which appears as "Præst". But
when I look at the resulting file afterwards, it has been
written as "Præst". I have also tried UTF7Encoding,
ASCIIEncoding and UnicodeEncoding, but to no avail.
Either just the special chars are are written incorrectly
or all chars are written incorrectly.

I just want to write with a standard Windows codepage.
Does anyone have a solution for this?


Regards,

Frank
 
F

Frank M.

Hi Jay,

Thanks for the help. As you can see I found the problem
to be with the output codepage.
It now works fine on both computers with the GetEncoding
("ISO8859-1"). Will probably also work fine with the
default that you gave.

Frank
 

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