|
|
Posted on 4/13/2018 4:11:20 PM
|
|
|

Today, when I was making an Open File dialog OpenFileDialog, I needed to read the open file and display it in the RichTextBox in the form. However, when displaying the file, it was found that the displayed Chinese string was garbled. The key parts of the code are as follows:
At first, I also thought that there was a problem with the character encoding, so when I initialized StreamReader, I added the character encoding parameters to it, and added it several times, Encoding.Unicode, Encoding.UTF32, Encoding.UTF7, Encoding.UTF8, Encoding.ASCII, I tried all the encoding schemes that could be added, but the result was not good.
Originally, the operating system from Windows 2000 and later used Unicode for file processing, so . The default file encoding for .Net is also Unicode. Unless otherwise specified, the default encoding of StreamReader is Unicode, not the current system's ANSI code page. However, most of the documents are still stored in ANSI encoding, and Chinese text uses gb2312, which causes Chinese garbled characters, which means that the encoding format must be specified when reading the text.
The workaround is: System.Text.Encoding.Default tells the StreamReader the encoding of the current operating system.
|
Previous:.net/c# Xml, Json, Hessian, Protocol Buffers serialization comparisonNext:.net/c# Use Costura.Fody to package the DLL into the EXE
|