There is a source with Russian comments, you must display its contents. Font Lucida, codepage Console.OutputEncoding = Encoding.UTF8. An attempt was made to convert a file from one encoding to another using the Convert () method. As far as I understand, in Visual Studio, the default utf-8 encoding (file-additionally-encoding is UTF-8). Instead of Cyrillic characters, other characters are output to the console (with any manipulations 0,65001,866 ...).

static void Main(string[] args) { FileStream fin=new FileStream("Program.cs", FileMode.Open); Console.OutputEncoding=Encoding.UTF8; int i; string s = ""; StringBuilder sb = new StringBuilder(); Encoding utf = Encoding.GetEncoding("utf-8"); Encoding smth = Encoding.GetEncoding(65001); //855, 866, 1251, 20866, 21866, 28595 ??? do { i=fin.ReadByte(); if (i!=-1) sb.Append((char)i); // удобно считываем файл } while (i!=-1); ///////////////////////////////////////////// for (int k = 0; k<sb.Length; k++) s+=sb[k]; // в string для GetBytes() byte[] utfBytes = utf.GetBytes(s.ToCharArray()); byte[] targetBytes = Encoding.Convert(utf, smth, utfBytes); // src, dst, byte[] //далее в char[] и выводим string char[] targetChars = new char[smth.GetCharCount(targetBytes, 0, targetBytes.Length)]; smth.GetChars(targetBytes, 0, targetBytes.Length, targetChars, 0); string res = new string(targetChars); ///////////////////////////////////////////// Console.WriteLine(res); } 
  • 2
    Why so hard? Check if your console displays the "привет" . If it works, output stupidly through foreach (var line in File.ReadLines(path)) Console.WriteLine(line); . - VladD

1 answer 1

It works without any additional manipulations in the Russian edition of Windows: enter image description here

Here is the code:

 static void Main(string[] args) { //Можно сделать так: //Console.OutputEncoding = Encoding.GetEncoding(1251); //Главное, чтобы в Свойствах -> Шрифт консольного окна не были выбраны «Точечные шрифты». // //Или же можно сделать так: //Console.OutputEncoding = Encoding.UTF8; //Но вполне себе работает и так вот: Console.WriteLine("Привет, stackoverflow.ru!"); Console.WriteLine("Сейчас напечатаем исходники, в которых есть кириллица."); Console.WriteLine(); string sourcePath = @"D:\Projects\D&R\ConsoleApplication2\ConsoleApplication2\Program.cs"; foreach (var L in File.ReadAllLines(sourcePath)) { Console.WriteLine(L); } Console.ReadLine(); } 

If your main system language is not Russian, the comments have lines with settings for the Console.OutputEncoding parameter. I think it will be enough to set for correct output the encoding in which you have recorded the file.

By default (as historically), the code page 866 is set in the window of the Russian-language Windows console.

  • the problem is that the files that are recorded from the IDE (it does not matter * .txt or * .dat) are not displayed, since they are stored in ASCII, which is confirmed if you open them in Notepad. You can, of course, open the notepad-save as-Unicode, then all the rules when reading. It turns out to read only symbolic i / o, but byte i / o does not work. - Jora Petrov
  • And what is your IDE? I did the example above in VS 2015 Community, under Win 7. And Notepad ++ tells me that the source files * .cs are stored in UTF-8 (c BOM). Far Manager says the same thing. And the notebook from Win 7 is the same: I open any * .cs file with a notebook, I can perfectly see Russian characters, click "Save as ..." in the menu and the notebook automatically suggests UTF-8 encoding in the save dialog. - BlackWitcher
  • And also - if your Visual Studio does not save files in UTF-8, then try to look at the question about the encoding and the corresponding answer to it. - BlackWitcher