简体   繁体   English

读取文件时的C#编码

[英]C# encoding when reading files

I have a file which contains letter ø . 我有一个包含字母ø的文件。 When I read from it with this code File.ReadLines(filePath) I got a question mark instead of it. 当我用这个代码File.ReadLines(filePath)读取它时,我得到一个问号而不是它。

And when I add Encoding like this File.ReadLines(filePath, Encoding.GetEncoding(1252)) I get the ø character. 当我像这个File.ReadLines(filePath, Encoding.GetEncoding(1252))一样添加Encoding时File.ReadLines(filePath, Encoding.GetEncoding(1252))我得到了ø字符。

But default Encoding is already set to 1252 , property Encoding.Default.CodePage returns 1252. 但默认编码已设置为1252 ,属性Encoding.Default.CodePage返回1252。

So why do I have to specify Encoding to 1252 while reading, when default one is already set to 1252 ? 那么为什么我必须在读取时指定编码到1252 ,当默认值已经设置为1252

And one more question, what if file is Unicode, will C# recognize its format or I have to specify Unicode encoding? 还有一个问题,如果文件是Unicode,C#会识别其格式还是我必须指定Unicode编码?

The reason is that by default the encoding used when reading text files is UTF8. 原因是默认情况下,读取文本文件时使用的编码是UTF8。

Encoding.Default is not (despite its name) the default encoding used when reading files! Encoding.Default不是(尽管它的名字)读取文件时使用的默认编码!

A much better name for Encoding.Default would have been Encoding.UsingCurrentCodePage , in my opinion. 对于一个更好的名字Encoding.Default本来Encoding.UsingCurrentCodePage ,在我看来。 ;) ;)

Also note that rather than using File.ReadLines(filePath, Encoding.GetEncoding(1252)) you could use File.ReadLines(filePath, Encoding.Default) . 另请注意,您可以使用File.ReadLines(filePath, Encoding.Default)而不是使用File.ReadLines(filePath, Encoding.GetEncoding(1252)) File.ReadLines(filePath, Encoding.Default)

You would do that if your code is trying to read files that have been created in a different code page than 1252, and that code page is the current code page for the system on which the code is running. 如果您的代码试图读取在1252之外的不同代码页中创建的文件,那么您将这样做,并且该代码页是运行代码的系统的当前代码页。

The only reason you should be using code pages is if you are reading or writing legacy files. 您应该使用代码页的唯一原因是您正在阅读或编写旧文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM