Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Swedish characters properly from a txt file

I am reading a file (line by line) full of Swedish characters like äåö but how can I read and save the strings with Swedish characters. Here is my code and I am using UTF8 encoding:

TextReader tr = new StreamReader(@"c:\testfile.txt", System.Text.Encoding.UTF8, true);
tr.ReadLine() //returns a string but Swedish characters are not appearing correctly...
like image 712
Geek Avatar asked Dec 13 '12 00:12

Geek


1 Answers

You need to change the System.Text.Encoding.UTF8 to System.Text.Encoding.GetEncoding(1252). See below

        System.IO.TextReader tr = new System.IO.StreamReader(@"c:\testfile.txt", System.Text.Encoding.GetEncoding(1252), true);
        tr.ReadLine(); //returns a string but Swedish characters are not appearing correctly
like image 167
Sorceri Avatar answered Sep 21 '22 15:09

Sorceri