How to read Swedish characters from a txt file

I am reading a file (line by line) full of Swedish characters like Γ€Γ₯ΓΆ, but how can I read and save lines 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... 
+8
source share
3 answers

You need to change 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 
+15
source

I myself realized that System.Text.Encoding.Default will support Swedish characters.

 TextReader tr = new StreamReader(@"c:\testfile.txt", System.Text.Encoding.Default, true); 
0
source

System.Text.Encoding.UTF8 should be enough, and it is supported both in the .NET Framework and in the .NET Core https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding? redirectedfrom = MSDN & view = NetFramework-4.8

If you still have problems with characters ((instead of Γ…Γ–Γ„), check the source file - what is its encoding? Maybe it's ANSI , then you will have to convert to UTF8 .

You can do this in Notepad ++. You can open the text file and go to Encoding - Convert to UTF-8.

Alternatively in the source code (C #):

 var myString = Encoding.UTF8.GetString(File.ReadAllBytes(pathToTheTextFile)); 
0
source

All Articles