我在一个旨在格式化我拥有的一些文件的程序中调用 File.ReadAllText()
。
其中一些文件包含 ®
(174) 符号。但是,当读取文本时,返回的字符串包含 �
(65533) 符号,而 ®
(174) 应该在其中。
什么会导致这种情况,我该如何解决?
最佳答案
文件很可能包含与默认编码不同的编码。如果您知道,可以使用 File.ReadAllText Method (String, Encoding) 指定它覆盖。
代码示例:
string readText = File.ReadAllText(path, Encoding.Default); // <-- change the encoding to whatever the encoding really is
如果您不知道编码,请参阅之前的 SO 问题:How to use ReadAllText when file encoding unknown
关于c# - File.ReadAllText 中的无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480970/