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...
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
I myself realized that System.Text.Encoding.Default will support Swedish characters.
System.Text.Encoding.Default
TextReader tr = new StreamReader(@"c:\testfile.txt", System.Text.Encoding.Default, true);
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
System.Text.Encoding.UTF8
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));