File.ReadAllText 中的无效字符 [英] Invalid characters in File.ReadAllText
问题描述
我正在一个程序中调用 File.ReadAllText()
来格式化我拥有的一些文件.
I'm calling File.ReadAllText()
in a program designed to format some files that I have.
其中一些文件包含 ®
(174) 符号.但是,在读取文本时,返回的字符串包含
(65533) 符号,其中 ®
(174) 应该是.
Some of these files contain the ®
(174) symbol. However, when the text is being read, the returned string contains �
(65533) symbols where the ®
(174) should be.
这是什么原因造成的,我该如何解决?
What would cause this and how can I fix it?
推荐答案
这可能是由于 Encoding
不匹配造成的.使用 ReadAllText 重载,它允许您指定正确的 Encoding
读取文件时使用.
This is likely due to a mismatch in the Encoding
. Use the ReadAllText overload which allows you to specify the proper Encoding
to use when reading the file.
除非可以检测到 UTF-32,否则默认重载将假定为 UTF-8.任何其他编码都会错误地通过.
The default overload will assume UTF-8 unless it can detect UTF-32. Any other encoding will come through incorrectly.
这篇关于File.ReadAllText 中的无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!