Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read text files with ANSI encoding and non-English letters?

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(); 
like image 286
MichaelT Avatar asked Aug 26 '12 12:08

MichaelT


People also ask

How do I read ANSI files?

Opening this log file in Notepad++, the menu Encoding indicates it is ANSI. If I copy/paste the content of the file in a new one where Notepad++ indicates that the encoding is UTF-8 and that I use the latter in my program, then it works well.

How can I convert ANSI to English?

Try Settings -> Preferences -> New document -> Encoding -> choose UTF-8 without BOM, and check Apply to opened ANSI files .


1 Answers

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

List of codepages : https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers?redirectedfrom=MSDN

like image 118
L.B Avatar answered Oct 05 '22 08:10

L.B