我尝试使用 StreamWriter 在 Windows-1252 中对字符串进行编码。输入字符串 (dataString) 采用 UTF8 编码。
StreamWriter sw = new StreamWriter(@"C:\Temp\data.txt", true, Encoding.GetEncoding(1252));
sw.Write(dataString);
sw.Close();
Run Code Online (Sandbox Code Playgroud)
当我在 Notepad++ 中打开该文件时,我得到一个 ANSI 文件。我需要一个 Windows-1252 编码文件。
有人有主意吗?
您的文件采用 Windows-1252 编码。非 Unicode 文件中没有数据来指示文件的编码方式。在这种情况下,ANSI 只是意味着不是 Unicode。如果您将其编码为 Russian/Windows-1251 并在 Notepad++ 中打开它,Notepad++ 也会将其显示为 ANSI。
有关详细信息,请参阅Unicode、UTF、ASCII、ANSI 格式差异。