.NET Core 2的编码问题 [英] Encoding issues with .net core 2
问题描述
此代码在 .net框架
中可以正常工作,但在 .net核心2
This code works as expected in .net framework
but not in .net core 2
.txt中的文件in.txt包含杜塞尔多夫
The file in.txt contains "Düsseldorf"
-
.net框架中的输出是.net核心中的杜塞尔多夫
in .net framework the output is "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?
推荐答案
根据官方 MSDN页面默认编码不是固定的-它取决于操作系统设置。如果您知道文件具有哪种编码,请指定它!
According to the official MSDN page the default encoding is not fixed - it depends on the OS settings. If you know which encoding the file has, specify it!
编辑:
然后尝试从.net框架中打印编码详细信息(如名称)作品。然后在.net core 2中指定相同的名称。不要依赖默认值。此页面 MSDN,列表代码示例中的编码包含受支持的编码列表。
Then try print the encoding details (like name) from the .net framework one that works. Then specify the same in .net core 2. Do not rely on the default one. This page MSDN, List of encodings in the code sample contains a list of encodings that are supported.
由gsharp更新:
我必须引用NuGet包 System.Text.Encoding.CodePages
,对其进行注册并使用
UPDATE by gsharp:
I had to reference the NuGet Package System.Text.Encoding.CodePages
, register them and use it
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc1252 = Encoding.GetEncoding(1252);
var inStr = new StreamReader(infile, enc1252);
var outStr = new StreamWriter(outFile, false, enc1252);
这篇关于.NET Core 2的编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!