如何阅读文本文件使用ANSI编码和非英文字母? [英] How to read text files with ANSI encoding and non-English letters?

查看:249
本文介绍了如何阅读文本文件使用ANSI编码和非英文字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含非英语字符,并使用非英语codePAGE保存在ANSI编码的文件。我怎样才能读取C#这个文件,并正确地看到该文件的内容?

不工作

  StreamReader的SR =新的StreamReader(@C:\ APPLICATIONS.xml,Encoding.ASCII);
变种AGS = sr.ReadToEnd();
SR =新的StreamReader(@C:\ APPLICATIONS.xml,Encoding.UTF8);
AGS = sr.ReadToEnd();
SR =新的StreamReader(@C:\ APPLICATIONS.xml,Encoding.Uni code);
AGS = sr.ReadToEnd();
 

工作,但我需要知道什么是code页面提前,这是不可能的。

  SR =新的StreamReader(@C:\ APPLICATIONS.xml,Encoding.GetEncoding(1252));
AGS = sr.ReadToEnd();
 

解决方案

  VAR文本= File.ReadAllText(文件,Encoding.GetEncoding(codePAGE));
 

的codepages

清单:<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx">http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx

I have a file that contains non-English chars and was saved in ANSI encoding using a non-English codepage. How can I read this file in C# and see the file content correctly?

Not working

StreamReader sr=new StreamReader(@"C:\APPLICATIONS.xml",Encoding.ASCII);
var ags = sr.ReadToEnd();
sr=new StreamReader(@"C:\APPLICATIONS.xml",Encoding.UTF8);
ags = sr.ReadToEnd();
sr=new StreamReader(@"C:\APPLICATIONS.xml",Encoding.Unicode);
ags = sr.ReadToEnd();

Working but I need to know what is the code page in advance, which is not possible.

sr=new StreamReader(@"C:\APPLICATIONS.xml",Encoding.GetEncoding(1252));
ags = sr.ReadToEnd();

解决方案

 var text = File.ReadAllText(file, Encoding.GetEncoding(codePage));

List of codepages : http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx

这篇关于如何阅读文本文件使用ANSI编码和非英文字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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