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();
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.
Try Settings -> Preferences -> New document -> Encoding -> choose UTF-8 without BOM, and check Apply to opened ANSI files .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With