How to use StreamWriter.WriteLine with non-american characters ?

  • Thread starter Thread starter ptek
  • Start date Start date
P

ptek

Hi,

For example, if I do :


using System.IO;

using (StreamWriter sw = new StreamWriter("text.txt"))
{
sw.WriteLine("coupé");
}

I end up with a file containing "coupé" instead of "coupé" ...
How can I solve this problem ?

thanks
 
Hi ptek,

The file is correct, but you read it wrong, probably using notepad or similar.
The default StreamReader/StreamWriter uses UTF8 encoding. What you probably want is to use the default encoding

using (StreamWriter sw = new StreamWriter("text.txt", false, Encoding.Default))
{
sw.WriteLine("coupé");
}

PS, encoding is found in the System.Text namespace.
 
Hi,


You just need to specify an encoding that has the desired characters.


using (StreamWriter sw = new
StreamWriter("text.txt",false,Encoding.Default))
{
sw.WriteLine("coupé");
}

Or

using (StreamWriter sw = new
StreamWriter("text.txt",false,Encoding.Unicode))
{
sw.WriteLine("coupé");
}

David
 
Hi,

You just need to specify an encoding that has the desired characters.

using (StreamWriter sw = new
StreamWriter("text.txt",false,Encoding.Default))
{
sw.WriteLine("coupé");
}

Or

using (StreamWriter sw = new
StreamWriter("text.txt",false,Encoding.Unicode))
{
sw.WriteLine("coupé");
}

David

Thanks all! It worked like a charm!
 

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

Back
Top