如何读取包含特殊字符的 ANSI 编码文件 [英] How to read an ANSI encoded file containing special characters

查看:36
本文介绍了如何读取包含特殊字符的 ANSI 编码文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 TFS Checkin 策略,用于检查我们的源文件是否包含我们的文件头.

I'm writing a TFS Checkin policy, which checks if our source files containing our file header.

我的问题是,我们的文件头包含一个特殊字符©",不幸的是,我们的一些源文件是用 ANSI 编码的.因此,如果我在策略中读取这些文件,字符串看起来像这样Copyright 2009".

My problem is, that our file header contains a special character "©" and unfortunately some of our source files are encoded in ANSI. So if I read these files in the policy, the string looks like this "Copyright � 2009".

string content = File.ReadAllText(pendingChange.LocalItem);

我厌倦了更改字符串的编码,但这无济于事.那么如何读取这些文件,才能获得正确的字符串Copyright © 2009"?

I tired to change the encoding of the string, but it does not help. So how can I read these files, that I get the correct string "Copyright © 2009"?

推荐答案

Use Encoding.Default:

string content = File.ReadAllText(pendingChange.LocalItem, Encoding.Default);

然而,您应该知道,它使用系统默认编码读取它 - 这可能与文件的编码不同.没有称为 ANSI 的单一编码,但通常当人们谈论ANSI 编码"时,他们指的是 Windows 代码页 1252 或他们的盒子碰巧使用的任何东西.

You should be aware, however, that that reads it using the system default encoding - which may not be the same as the encoding of the file. There's no single encoding called ANSI, but usually when people talk about "the ANSI encoding" they mean Windows Code Page 1252 or whatever their box happens to use.

如果您能找出所使用的确切编码,您的代码将会更加健壮.

Your code will be more robust if you can find out the exact encoding used.

这篇关于如何读取包含特殊字符的 ANSI 编码文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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