[英]Encoding issues with .net core 2
This code works as expected in .net framework
but not in .net core 2
此代码可以在.net framework
正常工作,但不能在.net core 2
The file in.txt contains "Düsseldorf" in.txt文件包含“杜塞尔多夫”
in .net framework the output is "Düsseldorf" 在.net框架中,输出为“Düsseldorf”
in .net core the output is "D sseldorf" 在.net核心中,输出为“ D.sseldorf”
(I've tried all the other Encodings out of desperation already... no one works) (我已经出于绝望而尝试了所有其他编码,没有人可以使用)
string infile = @"C:\in.txt", outFile = @"C:\out.txt";
var inStr = new StreamReader(infile, Encoding.Default);
var outStr = new StreamWriter(outFile, false, Encoding.Default);
while (!inStr.EndOfStream)
{
outStr.WriteLine(inStr.ReadLine());
}
outStr.Flush();
inStr.Dispose();
outStr.Dispose();
Any Ideas why it's not working? 任何想法为什么它不起作用?
According to the official MSDN page the default encoding is not fixed - it depends on the OS settings. 根据官方的MSDN页面 ,默认编码不是固定的-它取决于操作系统设置。 If you know which encoding the file has, specify it! 如果您知道文件具有哪种编码,请指定它!
EDIT: Then try print the encoding details (like name) from the .net framework one that works. 编辑:然后尝试从有效的.net框架中打印编码详细信息(如名称)。 Then specify the same in .net core 2. Do not rely on the default one. 然后在.net core 2中指定相同的名称。不要依赖默认值。 This page MSDN, List of encodings in the code sample contains a list of encodings that are supported. 此页面MSDN,代码示例中的编码列表包含受支持的编码列表。
UPDATE by gsharp: I had to reference the NuGet Package System.Text.Encoding.CodePages
, register them and use it gsharp的更新:我不得不引用NuGet包System.Text.Encoding.CodePages
,注册并使用它
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc1252 = Encoding.GetEncoding(1252);
var inStr = new StreamReader(infile, enc1252);
var outStr = new StreamWriter(outFile, false, enc1252);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.