Why Does Byte 150 show up as a dash in Notepad but Not when I read it programatically?

The File.ReadAllText(string) overload defaults to UTF8 encoding, in which a standalone byte with value 150 is invalid.

Specify the actual encoding of the file, for example:

var encoding = Encoding.GetEncoding(1252);
string content = File.ReadAllText(fileName, encoding);

I used the Windows-1252 encoding, which has a dash at codepoint 150.

Edit: Notepad displays the file correctly because for non-Unicode files the Windows-1252 codepage is the default for western regional settings. So likely you can use also Encoding.Default to get the correct result but keep in mind that Encoding.Default can return different code pages with different regional settings.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top