Encoding.Default 在 .NET 中如何工作? [英] How does Encoding.Default work in .NET?

查看:26
本文介绍了Encoding.Default 在 .NET 中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法读取文件:

I'm reading a file using:

var source = File.ReadAllText(path);

并且字符 © 没有被正确加载.

and the character © wasn't being loaded correctly.

然后,我将其更改为:

var source = File.ReadAllText(path, Encoding.UTF8);

什么都没有.

我决定尝试使用

var source = File.ReadAllText(path, Encoding.Default);

而且它工作得很好.然后我调试了它并试图找出哪个 Encoding 起作用了,我发现它是 UTF-7.

and it worked perfectly. Then I debugged it and tried to find which Encoding did the trick, and I found that it was UTF-7.

我想知道的是:

是否推荐使用Encoding.Default,是否可以保证文件的所有字符都可以正常读取?

Is it recommended to use Encoding.Default, and can it guarantee all the characters of the file will be read without problems?

推荐答案

Encoding.Default 将仅保证所有 UTF-7 字符集将被正确读取(谷歌搜索整个集).另一方面,如果您尝试在 UTF-8 模式下读取未使用 UTF-8 编码的文件,则会像您一样得到损坏的字符.

Encoding.Default will only guarantee that all UTF-7 character sets will be read correctly (google for the whole set). On the other hand, if you try to read a file not encoded with UTF-8 in the UTF-8 mode, you'll get corrupted characters like you did.

例如,如果文件是 UTF-16 编码的,并且您以 UTF-16 模式读取它,即使文件不包含单个 UTF-16 特定字符,您也会没事.这一切都归结为文件的编码.

For instance if the file is encoded UTF-16 and if you read it in UTF-16 mode, you'll be fine even if the file does not contain a single UTF-16 specific character. It all boils down to the file's encoding.

您需要进行保存 - 重新打开具有相同编码的内容以防止损坏.否则,请尽可能多地使用 UTF-7,因为它是最紧凑但电子邮件安全"的编码,这也是大多数 .NET 框架设置中的默认设置.

You'll need to do the save - reopen stuff with the same encoding to be safe from corruptions. Otherwise, try to use UTF-7 as much as you can since it is the most compact yet 'email safe' encoding possible, which is why it is default in most .NET framework setups.

这篇关于Encoding.Default 在 .NET 中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆