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

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

问题描述

我有一个包含非英语字符的文件,并使用非英语代码页以 ANSI 编码保存.如何在 C# 中读取此文件并正确查看文件内容?

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?

不工作

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));

代码页列表:https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers?redirectedfrom=MSDN

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

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