var myGoodString = System.IO.File.ReadAllText(
@"C:\path\to\file.txt",
Encoding.GetEncoding("Windows-1252")
);
A .NET/CLR string
in memory cannot be UTF-8. It is just Unicode, or UTF-16 if you like.
The above code will properly read a text file in CP1252 into a .NET string
.
If you insist on going through a byte[] wind1252Bytes
, it is simply:
var myGoodString = Encoding.GetEncoding("Windows-1252").GetString(wind1252Bytes);
Since this answer was written, new versions of the framework .NET have appeared which do not by default recognize all the old (legacy) Windows-specific code pages. If Encoding.GetEncoding("Windows-1252")
throws an exception with your runtime version, try registrering an additional provider with
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
(may need additional assembly reference to System.Text.Encoding.CodePages.dll
) before you use Encoding.GetEncoding("Windows-1252")
.
See CodePagesEncodingProvider
class documentation.