How to read Cyrillic characters from a .txt file using C #

I saw similar topics, but could not find a solution. My problem is that I have a .txt file in which the characters are in Bulgarian / which is Cyrillic /, but after trying to read them, there is no success. I tried to read this code:

StreamReader reader = new StreamReader(fileName,Encoding.UTF8);

if (File.Exists(fileName))
{
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

And I also changed the Encoding value to everything possible, as I tried with GetEncoding (1251), which I wrote for the Cyrillic alphabet. And when I save the .txt file, I tried to save it with every different encoding that was there / UNICODE, UTF-8, BigEndianUnicode, ANSI / in every combination with the encoding that I set through the code, but again without success.

Any ideas on how to read Cyrillic characters in the right direction will be appreciated. And here is a sample text for this: "This is a sample text."

Thanks in advance!:)

+2
source share
2 answers

Your problem is that the console cannot display Cyrillic characters. Try setting a breakpoint on Console.WriteLineand check the variable line. Clearly you need to know the correct encoding first! :-)

If you don't trust me, try this: create a console program that does this:

string line = "  "; 
Console.WriteLine(line);
return 0;

set a breakpoint on return 0;, look at the console and look at the string variable.

, Unicode "" .NET 4.5

: unicode #

+5

, , - , .

Unicode , . , , WinForms WPF, - Unicode .

Unicode. :

  • UTF8.
  • : cmd \u
  • "Lucida Sans Unicode": → →
  • Unicode: chcp 65001
  • .

:

enter image description here

+2

All Articles