Displaying Extended Latin-1 characters in a multi-lineTextbox

P

Paul Bradshaw

I have a weird situation...

I have a unicode text file that contains Latin-1 characters. This code displays fine in unicode text editors, and when converted to ANSI by a tool like EditPad, it looks correct as well. But when I use the code below to load it into a Text box, all the extended characters are stripped ... either the accents are missing, or the character is just plain gone.

using (StreamReader text = File.OpenText(logFilePath))
{
string log = text.ReadToEnd();
LogFileDisplay.Text = log;
}

I'm not sure what I'm missing here. Somewhere, something is transcoding the unicode text... and I want it to just leave it alone, and display it as-is. Any pointers?



EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
J

Jon Skeet [C# MVP]

I have a weird situation...

I have a unicode text file that contains Latin-1 characters. This
code displays fine in unicode text editors, and when converted to
ANSI by a tool like EditPad, it looks correct as well. But when I use
the code below to load it into a Text box, all the extended
characters are stripped ... either the accents are missing, or the
character is just plain gone.

using (StreamReader text = File.OpenText(logFilePath))
{
string log = text.ReadToEnd();
LogFileDisplay.Text = log;
}

I'm not sure what I'm missing here. Somewhere, something is
transcoding the unicode text... and I want it to just leave it alone,
and display it as-is. Any pointers?

Files are binary. Strings are text. When you convert between them, you
will *always* end up using an encoding somewhere, even if you think
it's just a "null" encoding.

In the case of StreamReader, the default encoding is UTF-8. If you want
to use a different encoding, you need to specify that. It's a shame
that File.OpenText neither documents what encoding it will use, nor
allows you to specify it.

Use one of the StreamReader constructors instead, either passing in the
filename or opening a stream and then passing that it - along with the
appropriate encoding.
 

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