C # Help reading foreign characters using StreamReader

I use the code below to read a text file containing foreign characters, the file is ANSI encoded and looks great in notepad. The code below does not work when the file values ​​are read and displayed in the datagrid, the characters are displayed as squares, maybe there is another problem elsewhere?

StreamReader reader = new StreamReader(inputFilePath, System.Text.Encoding.ANSI); using (reader = File.OpenText(inputFilePath)) 

thank

Update 1 . I tried all the encodings found in System.Text.Encoding . and all do not display the file correctly.

Update 2 . I changed the encoding of the file (saved the file) to unicode and used System.Text.Encoding.Unicode , and it worked fine. So why did the notebook read it correctly? And why didn't System.Text.Encoding.Unicode read the ANSI file?

+56
c # encoding
Feb 26 '09 at 22:55
source share
10 answers

Yes, it could be with the actual encoding of the file, possibly with unicode. Try UTF-8 as this is the most common form of Unicode encoding. Otherwise, if the file is ASCII, then standard ASCII encoding should work.

+23
Feb 26 '09 at 22:57
source share

You can also try the default encoding, which uses the current ANSI system codepage.

 StreamReader reader = new StreamReader(inputFilePath, Encoding.Default, true) 

When you try to use the Save As menu in Notepad with the source file, look at the encoding list box. It will tell you which encoded notepad the file guesses.

Also, if it is an ANSI file, the detectEncodingFromByteOrderMarks parameter will probably not help.

+120
Feb 26 '09 at 23:25
source share

I had the same problem and my solution was simple: instead

 Encoding.ASCII 

using

 Encoding.GetEncoding("iso-8859-1") 

The answer was found here .

Edit: more solutions. This may be more accurate:

 Encoding.GetEncoding(1252); 

In addition, in some cases this will work for you if your default encoding for the OS matches the encoding of the file:

 Encoding.Default; 
+21
Feb 07 '13 at 18:05
source share

Using Encoding.Unicode will not accurately decode an ANSI file in the same way a JPEG decoder will not understand a GIF file.

I am surprised that Encoding.Default did not work for the ANSI file, if it really was ANSI - if you ever found out which Notepad block page you are using, you can use Encoding.GetEncoding(int) .

In general, if possible, I would recommend using UTF-8.

+9
Feb 26 '09 at 23:29
source share

Try using a different encoding such as Encoding.UTF8. You can also try letting StreamReader find the encoding:

  StreamReader reader = new StreamReader(inputFilePath, System.Text.Encoding.UTF8, true) 

Edit: just saw your update. Try letting StreamReader do fortune telling.

+7
Feb 26 '09 at 22:59
source share

File.OpenText () always uses the UTF-8 StreamReader implicitly. Create your own StreamReader, instead specify the desired encoding. as

 using (StreamReader reader = new StreamReader(@"C:\test.txt", Encoding.Default) { // ... } 
+3
Apr 17 '12 at 7:24
source share

I solved the problem of reading Portuguese characters by changing the source file in notepad ++.

enter image description here

FROM#

  var url = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/data.json"); string s = string.Empty; using (System.IO.StreamReader sr = new System.IO.StreamReader(url, System.Text.Encoding.UTF8,true)) { s = sr.ReadToEnd(); } 
0
Jan 16 '15 at 2:31 on
source share

for Arabic, I used Encoding.GetEncoding(1256) . It works well.

0
Nov 29 '16 at 8:04
source share

For Swedish Γ… Γ–, the only solution from the above workers was:

 Encoding.GetEncoding("iso-8859-1") 

Hope this saves someone time.

0
Jun 12 '19 at 13:42 on
source share

I also read an exported file that contains French and German. I used Encoding.GetEncoding ("iso-8859-1"), though that worked without any problems.

0
Jun 24 '19 at 14:12
source share



All Articles